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 | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 25 | #include <asm/kvm_emulate.h> |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 26 | #include <linux/stringify.h> |
Nadav Amit | 3db176d | 2015-04-19 21:12:59 +0300 | [diff] [blame] | 27 | #include <asm/debugreg.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 | a994554 | 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 | a994554 | 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 | a994554 | 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 | a994554 | 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 | a994554 | 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 | a994554 | 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 */ |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 174 | #define Aligned16 ((u64)1 << 55) /* Aligned to 16 byte boundary (e.g. FXSAVE) */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 175 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 176 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 177 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 178 | #define X2(x...) x, x |
| 179 | #define X3(x...) X2(x), x |
| 180 | #define X4(x...) X2(x), X2(x) |
| 181 | #define X5(x...) X4(x), x |
| 182 | #define X6(x...) X4(x), X2(x) |
| 183 | #define X7(x...) X4(x), X3(x) |
| 184 | #define X8(x...) X4(x), X4(x) |
| 185 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 186 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 187 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 188 | #define FASTOP_SIZE 8 |
| 189 | |
| 190 | /* |
| 191 | * fastop functions have a special calling convention: |
| 192 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 193 | * dst: rax (in/out) |
| 194 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 195 | * src2: rcx (in) |
| 196 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 197 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 198 | * |
| 199 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 200 | * different operand sizes can be reached by calculation, rather than a jump |
| 201 | * table (which would be bigger than the code). |
| 202 | * |
| 203 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 204 | * so they can't be called from C directly. |
| 205 | */ |
| 206 | |
| 207 | struct fastop; |
| 208 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 209 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 210 | u64 flags : 56; |
| 211 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 212 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 213 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 214 | const struct opcode *group; |
| 215 | const struct group_dual *gdual; |
| 216 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 217 | const struct escape *esc; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 218 | const struct instr_dual *idual; |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 219 | const struct mode_dual *mdual; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 220 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 221 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 222 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | struct group_dual { |
| 226 | struct opcode mod012[8]; |
| 227 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 228 | }; |
| 229 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 230 | struct gprefix { |
| 231 | struct opcode pfx_no; |
| 232 | struct opcode pfx_66; |
| 233 | struct opcode pfx_f2; |
| 234 | struct opcode pfx_f3; |
| 235 | }; |
| 236 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 237 | struct escape { |
| 238 | struct opcode op[8]; |
| 239 | struct opcode high[64]; |
| 240 | }; |
| 241 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 242 | struct instr_dual { |
| 243 | struct opcode mod012; |
| 244 | struct opcode mod3; |
| 245 | }; |
| 246 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 247 | struct mode_dual { |
| 248 | struct opcode mode32; |
| 249 | struct opcode mode64; |
| 250 | }; |
| 251 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 252 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 253 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 254 | enum x86_transfer_type { |
| 255 | X86_TRANSFER_NONE, |
| 256 | X86_TRANSFER_CALL_JMP, |
| 257 | X86_TRANSFER_RET, |
| 258 | X86_TRANSFER_TASK_SWITCH, |
| 259 | }; |
| 260 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 261 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 262 | { |
| 263 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 264 | ctxt->regs_valid |= 1 << nr; |
| 265 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 266 | } |
| 267 | return ctxt->_regs[nr]; |
| 268 | } |
| 269 | |
| 270 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 271 | { |
| 272 | ctxt->regs_valid |= 1 << nr; |
| 273 | ctxt->regs_dirty |= 1 << nr; |
| 274 | return &ctxt->_regs[nr]; |
| 275 | } |
| 276 | |
| 277 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 278 | { |
| 279 | reg_read(ctxt, nr); |
| 280 | return reg_write(ctxt, nr); |
| 281 | } |
| 282 | |
| 283 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 284 | { |
| 285 | unsigned reg; |
| 286 | |
| 287 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 288 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 289 | } |
| 290 | |
| 291 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 292 | { |
| 293 | ctxt->regs_dirty = 0; |
| 294 | ctxt->regs_valid = 0; |
| 295 | } |
| 296 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 297 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 298 | * These EFLAGS bits are restored from saved value during emulation, and |
| 299 | * any changes are written back to the saved value after emulation. |
| 300 | */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 301 | #define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\ |
| 302 | X86_EFLAGS_PF|X86_EFLAGS_CF) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 303 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_X86_64 |
| 305 | #define ON64(x) x |
| 306 | #else |
| 307 | #define ON64(x) |
| 308 | #endif |
| 309 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 310 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 311 | |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 312 | #define FOP_FUNC(name) \ |
| 313 | ".align " __stringify(FASTOP_SIZE) " \n\t" \ |
| 314 | ".type " name ", @function \n\t" \ |
| 315 | name ":\n\t" |
| 316 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 317 | #define FOP_RET "ret \n\t" |
| 318 | |
| 319 | #define FOP_START(op) \ |
| 320 | extern void em_##op(struct fastop *fake); \ |
| 321 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 322 | ".global em_" #op " \n\t" \ |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 323 | FOP_FUNC("em_" #op) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 324 | |
| 325 | #define FOP_END \ |
| 326 | ".popsection") |
| 327 | |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 328 | #define FOPNOP() \ |
| 329 | FOP_FUNC(__stringify(__UNIQUE_ID(nop))) \ |
| 330 | FOP_RET |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 331 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 332 | #define FOP1E(op, dst) \ |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 333 | FOP_FUNC(#op "_" #dst) \ |
| 334 | "10: " #op " %" #dst " \n\t" FOP_RET |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 335 | |
| 336 | #define FOP1EEX(op, dst) \ |
| 337 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 338 | |
| 339 | #define FASTOP1(op) \ |
| 340 | FOP_START(op) \ |
| 341 | FOP1E(op##b, al) \ |
| 342 | FOP1E(op##w, ax) \ |
| 343 | FOP1E(op##l, eax) \ |
| 344 | ON64(FOP1E(op##q, rax)) \ |
| 345 | FOP_END |
| 346 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 347 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 348 | #define FASTOP1SRC2(op, name) \ |
| 349 | FOP_START(name) \ |
| 350 | FOP1E(op, cl) \ |
| 351 | FOP1E(op, cx) \ |
| 352 | FOP1E(op, ecx) \ |
| 353 | ON64(FOP1E(op, rcx)) \ |
| 354 | FOP_END |
| 355 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 356 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 357 | #define FASTOP1SRC2EX(op, name) \ |
| 358 | FOP_START(name) \ |
| 359 | FOP1EEX(op, cl) \ |
| 360 | FOP1EEX(op, cx) \ |
| 361 | FOP1EEX(op, ecx) \ |
| 362 | ON64(FOP1EEX(op, rcx)) \ |
| 363 | FOP_END |
| 364 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 365 | #define FOP2E(op, dst, src) \ |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 366 | FOP_FUNC(#op "_" #dst "_" #src) \ |
| 367 | #op " %" #src ", %" #dst " \n\t" FOP_RET |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 368 | |
| 369 | #define FASTOP2(op) \ |
| 370 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 371 | FOP2E(op##b, al, dl) \ |
| 372 | FOP2E(op##w, ax, dx) \ |
| 373 | FOP2E(op##l, eax, edx) \ |
| 374 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 375 | FOP_END |
| 376 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 377 | /* 2 operand, word only */ |
| 378 | #define FASTOP2W(op) \ |
| 379 | FOP_START(op) \ |
| 380 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 381 | FOP2E(op##w, ax, dx) \ |
| 382 | FOP2E(op##l, eax, edx) \ |
| 383 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 384 | FOP_END |
| 385 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 386 | /* 2 operand, src is CL */ |
| 387 | #define FASTOP2CL(op) \ |
| 388 | FOP_START(op) \ |
| 389 | FOP2E(op##b, al, cl) \ |
| 390 | FOP2E(op##w, ax, cl) \ |
| 391 | FOP2E(op##l, eax, cl) \ |
| 392 | ON64(FOP2E(op##q, rax, cl)) \ |
| 393 | FOP_END |
| 394 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 395 | /* 2 operand, src and dest are reversed */ |
| 396 | #define FASTOP2R(op, name) \ |
| 397 | FOP_START(name) \ |
| 398 | FOP2E(op##b, dl, al) \ |
| 399 | FOP2E(op##w, dx, ax) \ |
| 400 | FOP2E(op##l, edx, eax) \ |
| 401 | ON64(FOP2E(op##q, rdx, rax)) \ |
| 402 | FOP_END |
| 403 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 404 | #define FOP3E(op, dst, src, src2) \ |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 405 | FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \ |
| 406 | #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 407 | |
| 408 | /* 3-operand, word-only, src2=cl */ |
| 409 | #define FASTOP3WCL(op) \ |
| 410 | FOP_START(op) \ |
| 411 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 412 | FOP3E(op##w, ax, dx, cl) \ |
| 413 | FOP3E(op##l, eax, edx, cl) \ |
| 414 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 415 | FOP_END |
| 416 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 417 | /* Special case for SETcc - 1 instruction per cc */ |
Josh Poimboeuf | 1482a08 | 2016-01-21 16:49:29 -0600 | [diff] [blame] | 418 | #define FOP_SETCC(op) \ |
| 419 | ".align 4 \n\t" \ |
| 420 | ".type " #op ", @function \n\t" \ |
| 421 | #op ": \n\t" \ |
| 422 | #op " %al \n\t" \ |
| 423 | FOP_RET |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 424 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 425 | asm(".global kvm_fastop_exception \n" |
| 426 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 427 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 428 | FOP_START(setcc) |
| 429 | FOP_SETCC(seto) |
| 430 | FOP_SETCC(setno) |
| 431 | FOP_SETCC(setc) |
| 432 | FOP_SETCC(setnc) |
| 433 | FOP_SETCC(setz) |
| 434 | FOP_SETCC(setnz) |
| 435 | FOP_SETCC(setbe) |
| 436 | FOP_SETCC(setnbe) |
| 437 | FOP_SETCC(sets) |
| 438 | FOP_SETCC(setns) |
| 439 | FOP_SETCC(setp) |
| 440 | FOP_SETCC(setnp) |
| 441 | FOP_SETCC(setl) |
| 442 | FOP_SETCC(setnl) |
| 443 | FOP_SETCC(setle) |
| 444 | FOP_SETCC(setnle) |
| 445 | FOP_END; |
| 446 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 447 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 448 | FOP_END; |
| 449 | |
Radim Krčmář | aae8f34 | 2016-11-08 20:54:18 +0100 | [diff] [blame] | 450 | /* |
| 451 | * XXX: inoutclob user must know where the argument is being expanded. |
| 452 | * Relying on CC_HAVE_ASM_GOTO would allow us to remove _fault. |
| 453 | */ |
| 454 | #define asm_safe(insn, inoutclob...) \ |
| 455 | ({ \ |
| 456 | int _fault = 0; \ |
| 457 | \ |
| 458 | asm volatile("1:" insn "\n" \ |
| 459 | "2:\n" \ |
| 460 | ".pushsection .fixup, \"ax\"\n" \ |
| 461 | "3: movl $1, %[_fault]\n" \ |
| 462 | " jmp 2b\n" \ |
| 463 | ".popsection\n" \ |
| 464 | _ASM_EXTABLE(1b, 3b) \ |
| 465 | : [_fault] "+qm"(_fault) inoutclob ); \ |
| 466 | \ |
| 467 | _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \ |
| 468 | }) |
| 469 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 470 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 471 | enum x86_intercept intercept, |
| 472 | enum x86_intercept_stage stage) |
| 473 | { |
| 474 | struct x86_instruction_info info = { |
| 475 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 476 | .rep_prefix = ctxt->rep_prefix, |
| 477 | .modrm_mod = ctxt->modrm_mod, |
| 478 | .modrm_reg = ctxt->modrm_reg, |
| 479 | .modrm_rm = ctxt->modrm_rm, |
| 480 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 481 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 482 | .src_bytes = ctxt->src.bytes, |
| 483 | .dst_bytes = ctxt->dst.bytes, |
| 484 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 485 | .next_rip = ctxt->eip, |
| 486 | }; |
| 487 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 488 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 491 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 492 | { |
| 493 | *dest = (*dest & ~mask) | (src & mask); |
| 494 | } |
| 495 | |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 496 | static void assign_register(unsigned long *reg, u64 val, int bytes) |
| 497 | { |
| 498 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 499 | switch (bytes) { |
| 500 | case 1: |
| 501 | *(u8 *)reg = (u8)val; |
| 502 | break; |
| 503 | case 2: |
| 504 | *(u16 *)reg = (u16)val; |
| 505 | break; |
| 506 | case 4: |
| 507 | *reg = (u32)val; |
| 508 | break; /* 64b: zero-extend */ |
| 509 | case 8: |
| 510 | *reg = val; |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 515 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 516 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 517 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 520 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 521 | { |
| 522 | u16 sel; |
| 523 | struct desc_struct ss; |
| 524 | |
| 525 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 526 | return ~0UL; |
| 527 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 528 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 529 | } |
| 530 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 531 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 532 | { |
| 533 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 534 | } |
| 535 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 536 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 537 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 538 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 539 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 540 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 541 | return reg; |
| 542 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 543 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static inline unsigned long |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 547 | register_address(struct x86_emulate_ctxt *ctxt, int reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 548 | { |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 549 | return address_mask(ctxt, reg_read(ctxt, reg)); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 552 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 553 | { |
| 554 | assign_masked(reg, *reg + inc, mask); |
| 555 | } |
| 556 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 557 | static inline void |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 558 | register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 559 | { |
Nadav Amit | ee122a7 | 2015-04-28 13:06:00 +0300 | [diff] [blame] | 560 | ulong *preg = reg_rmw(ctxt, reg); |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 561 | |
Nadav Amit | ee122a7 | 2015-04-28 13:06:00 +0300 | [diff] [blame] | 562 | assign_register(preg, *preg + inc, ctxt->ad_bytes); |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 566 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 567 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 568 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 569 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 570 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 571 | { |
| 572 | u32 limit = get_desc_limit(desc); |
| 573 | |
| 574 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 575 | } |
| 576 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 577 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 578 | { |
| 579 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 580 | return 0; |
| 581 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 582 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 583 | } |
| 584 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 585 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 586 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 587 | { |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 588 | WARN_ON(vec > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 589 | ctxt->exception.vector = vec; |
| 590 | ctxt->exception.error_code = error; |
| 591 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 592 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 593 | } |
| 594 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 595 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 596 | { |
| 597 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 598 | } |
| 599 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 600 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 601 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 602 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 603 | } |
| 604 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 605 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 606 | { |
| 607 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 608 | } |
| 609 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 610 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 611 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 612 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 613 | } |
| 614 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 615 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 616 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 617 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 618 | } |
| 619 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 620 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 621 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 622 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 623 | } |
| 624 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 625 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 626 | { |
| 627 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 628 | } |
| 629 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 630 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 631 | { |
| 632 | u16 selector; |
| 633 | struct desc_struct desc; |
| 634 | |
| 635 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 636 | return selector; |
| 637 | } |
| 638 | |
| 639 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 640 | unsigned seg) |
| 641 | { |
| 642 | u16 dummy; |
| 643 | u32 base3; |
| 644 | struct desc_struct desc; |
| 645 | |
| 646 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 647 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 648 | } |
| 649 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 650 | /* |
| 651 | * x86 defines three classes of vector instructions: explicitly |
| 652 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 653 | * depending on whether they're AVX encoded or not. |
| 654 | * |
| 655 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 656 | * subject to the same check. FXSAVE and FXRSTOR are checked here too as their |
| 657 | * 512 bytes of data must be aligned to a 16 byte boundary. |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 658 | */ |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 659 | static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size) |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 660 | { |
| 661 | if (likely(size < 16)) |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 662 | return 1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 663 | |
| 664 | if (ctxt->d & Aligned) |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 665 | return size; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 666 | else if (ctxt->d & Unaligned) |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 667 | return 1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 668 | else if (ctxt->d & Avx) |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 669 | return 1; |
| 670 | else if (ctxt->d & Aligned16) |
| 671 | return 16; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 672 | else |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 673 | return size; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 674 | } |
| 675 | |
Paolo Bonzini | d09155d | 2014-10-27 14:54:44 +0100 | [diff] [blame] | 676 | static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, |
| 677 | struct segmented_address addr, |
| 678 | unsigned *max_size, unsigned size, |
| 679 | bool write, bool fetch, |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 680 | enum x86emul_mode mode, ulong *linear) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 681 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 682 | struct desc_struct desc; |
| 683 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 684 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 685 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 686 | u16 sel; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 687 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 688 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 689 | *max_size = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 690 | switch (mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 691 | case X86EMUL_MODE_PROT64: |
Paolo Bonzini | 0c1d77f | 2016-02-19 18:07:21 +0100 | [diff] [blame] | 692 | *linear = la; |
Nadav Amit | 4be4de7 | 2014-09-18 22:39:40 +0300 | [diff] [blame] | 693 | if (is_noncanonical_address(la)) |
Nadav Amit | abc7d8a | 2014-11-19 17:43:12 +0200 | [diff] [blame] | 694 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 695 | |
| 696 | *max_size = min_t(u64, ~0u, (1ull << 48) - la); |
| 697 | if (size > *max_size) |
| 698 | goto bad; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 699 | break; |
| 700 | default: |
Paolo Bonzini | 0c1d77f | 2016-02-19 18:07:21 +0100 | [diff] [blame] | 701 | *linear = la = (u32)la; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 702 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 703 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 704 | if (!usable) |
| 705 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 706 | /* code segment in protected mode or read-only data segment */ |
| 707 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 708 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 709 | goto bad; |
| 710 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 711 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 712 | goto bad; |
| 713 | lim = desc_limit_scaled(&desc); |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 714 | if (!(desc.type & 8) && (desc.type & 4)) { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 715 | /* expand-down segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 716 | if (addr.ea <= lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 717 | goto bad; |
| 718 | lim = desc.d ? 0xffffffff : 0xffff; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 719 | } |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 720 | if (addr.ea > lim) |
| 721 | goto bad; |
Nadav Amit | bac15531 | 2015-01-26 09:32:26 +0200 | [diff] [blame] | 722 | if (lim == 0xffffffff) |
| 723 | *max_size = ~0u; |
| 724 | else { |
| 725 | *max_size = (u64)lim + 1 - addr.ea; |
| 726 | if (size > *max_size) |
| 727 | goto bad; |
| 728 | } |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 729 | break; |
| 730 | } |
Radim Krčmář | bc5e131 | 2016-11-08 20:54:16 +0100 | [diff] [blame] | 731 | if (la & (insn_alignment(ctxt, size) - 1)) |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 732 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 733 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 734 | bad: |
| 735 | if (addr.seg == VCPU_SREG_SS) |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 736 | return emulate_ss(ctxt, 0); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 737 | else |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 738 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 739 | } |
| 740 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 741 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 742 | struct segmented_address addr, |
| 743 | unsigned size, bool write, |
| 744 | ulong *linear) |
| 745 | { |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 746 | unsigned max_size; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 747 | return __linearize(ctxt, addr, &max_size, size, write, false, |
| 748 | ctxt->mode, linear); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 751 | static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 752 | enum x86emul_mode mode) |
| 753 | { |
| 754 | ulong linear; |
| 755 | int rc; |
| 756 | unsigned max_size; |
| 757 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 758 | .ea = dst }; |
| 759 | |
| 760 | if (ctxt->op_bytes != sizeof(unsigned long)) |
| 761 | addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1); |
| 762 | rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear); |
| 763 | if (rc == X86EMUL_CONTINUE) |
| 764 | ctxt->_eip = addr.ea; |
| 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst) |
| 769 | { |
| 770 | return assign_eip(ctxt, dst, ctxt->mode); |
| 771 | } |
| 772 | |
| 773 | static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 774 | const struct desc_struct *cs_desc) |
| 775 | { |
| 776 | enum x86emul_mode mode = ctxt->mode; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 777 | int rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 778 | |
| 779 | #ifdef CONFIG_X86_64 |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 780 | if (ctxt->mode >= X86EMUL_MODE_PROT16) { |
| 781 | if (cs_desc->l) { |
| 782 | u64 efer = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 783 | |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 784 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 785 | if (efer & EFER_LMA) |
| 786 | mode = X86EMUL_MODE_PROT64; |
| 787 | } else |
| 788 | mode = X86EMUL_MODE_PROT32; /* temporary value */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 789 | } |
| 790 | #endif |
| 791 | if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32) |
| 792 | mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 793 | rc = assign_eip(ctxt, dst, mode); |
| 794 | if (rc == X86EMUL_CONTINUE) |
| 795 | ctxt->mode = mode; |
| 796 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
| 800 | { |
| 801 | return assign_eip_near(ctxt, ctxt->_eip + rel); |
| 802 | } |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 803 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 804 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 805 | struct segmented_address addr, |
| 806 | void *data, |
| 807 | unsigned size) |
| 808 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 809 | int rc; |
| 810 | ulong linear; |
| 811 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 812 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 813 | if (rc != X86EMUL_CONTINUE) |
| 814 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 815 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Steve Rutherford | 736e77c0 | 2017-01-11 18:28:29 -0800 | [diff] [blame] | 818 | static int segmented_write_std(struct x86_emulate_ctxt *ctxt, |
| 819 | struct segmented_address addr, |
| 820 | void *data, |
| 821 | unsigned int size) |
| 822 | { |
| 823 | int rc; |
| 824 | ulong linear; |
| 825 | |
| 826 | rc = linearize(ctxt, addr, size, true, &linear); |
| 827 | if (rc != X86EMUL_CONTINUE) |
| 828 | return rc; |
| 829 | return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception); |
| 830 | } |
| 831 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 832 | /* |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 833 | * Prefetch the remaining bytes of the instruction without crossing page |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 834 | * boundary if they are not in fetch_cache yet. |
| 835 | */ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 836 | 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] | 837 | { |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 838 | int rc; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 839 | unsigned size, max_size; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 840 | unsigned long linear; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 841 | int cur_size = ctxt->fetch.end - ctxt->fetch.data; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 842 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 843 | .ea = ctxt->eip + cur_size }; |
| 844 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 845 | /* |
| 846 | * We do not know exactly how many bytes will be needed, and |
| 847 | * __linearize is expensive, so fetch as much as possible. We |
| 848 | * just have to avoid going beyond the 15 byte limit, the end |
| 849 | * of the segment, or the end of the page. |
| 850 | * |
| 851 | * __linearize is called with size 0 so that it does not do any |
| 852 | * boundary check itself. Instead, we use max_size to check |
| 853 | * against op_size. |
| 854 | */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 855 | rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode, |
| 856 | &linear); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 857 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 858 | return rc; |
| 859 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 860 | size = min_t(unsigned, 15UL ^ cur_size, max_size); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 861 | size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear)); |
Paolo Bonzini | 5cfc7e0 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * One instruction can only straddle two pages, |
| 865 | * and one has been loaded at the beginning of |
| 866 | * x86_decode_insn. So, if not enough bytes |
| 867 | * still, we must have hit the 15-byte boundary. |
| 868 | */ |
| 869 | if (unlikely(size < op_size)) |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 870 | return emulate_gp(ctxt, 0); |
| 871 | |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 872 | rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 873 | size, &ctxt->exception); |
| 874 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 875 | return rc; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 876 | ctxt->fetch.end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 877 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 880 | static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, |
| 881 | unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 882 | { |
Nadav Amit | 08da44a | 2014-10-03 01:10:04 +0300 | [diff] [blame] | 883 | unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr; |
| 884 | |
| 885 | if (unlikely(done_size < size)) |
| 886 | return __do_insn_fetch_bytes(ctxt, size - done_size); |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 887 | else |
| 888 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 889 | } |
| 890 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 891 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 892 | #define insn_fetch(_type, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 893 | ({ _type _x; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 894 | \ |
| 895 | rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 896 | if (rc != X86EMUL_CONTINUE) \ |
| 897 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 898 | ctxt->_eip += sizeof(_type); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 899 | _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \ |
| 900 | ctxt->fetch.ptr += sizeof(_type); \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 901 | _x; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 902 | }) |
| 903 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 904 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 905 | ({ \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 906 | rc = do_insn_fetch_bytes(_ctxt, _size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 907 | if (rc != X86EMUL_CONTINUE) \ |
| 908 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 909 | ctxt->_eip += (_size); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 910 | memcpy(_arr, ctxt->fetch.ptr, _size); \ |
| 911 | ctxt->fetch.ptr += (_size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 912 | }) |
| 913 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 914 | /* |
| 915 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 916 | * pointer into the block that addresses the relevant register. |
| 917 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 918 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 919 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 920 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | { |
| 922 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 923 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 924 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 925 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 926 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 927 | else |
| 928 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | return p; |
| 930 | } |
| 931 | |
| 932 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 933 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | u16 *size, unsigned long *address, int op_bytes) |
| 935 | { |
| 936 | int rc; |
| 937 | |
| 938 | if (op_bytes == 2) |
| 939 | op_bytes = 3; |
| 940 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 941 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 942 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 943 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 944 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 945 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 946 | return rc; |
| 947 | } |
| 948 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 949 | FASTOP2(add); |
| 950 | FASTOP2(or); |
| 951 | FASTOP2(adc); |
| 952 | FASTOP2(sbb); |
| 953 | FASTOP2(and); |
| 954 | FASTOP2(sub); |
| 955 | FASTOP2(xor); |
| 956 | FASTOP2(cmp); |
| 957 | FASTOP2(test); |
| 958 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 959 | FASTOP1SRC2(mul, mul_ex); |
| 960 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 961 | FASTOP1SRC2EX(div, div_ex); |
| 962 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 963 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 964 | FASTOP3WCL(shld); |
| 965 | FASTOP3WCL(shrd); |
| 966 | |
| 967 | FASTOP2W(imul); |
| 968 | |
| 969 | FASTOP1(not); |
| 970 | FASTOP1(neg); |
| 971 | FASTOP1(inc); |
| 972 | FASTOP1(dec); |
| 973 | |
| 974 | FASTOP2CL(rol); |
| 975 | FASTOP2CL(ror); |
| 976 | FASTOP2CL(rcl); |
| 977 | FASTOP2CL(rcr); |
| 978 | FASTOP2CL(shl); |
| 979 | FASTOP2CL(shr); |
| 980 | FASTOP2CL(sar); |
| 981 | |
| 982 | FASTOP2W(bsf); |
| 983 | FASTOP2W(bsr); |
| 984 | FASTOP2W(bt); |
| 985 | FASTOP2W(bts); |
| 986 | FASTOP2W(btr); |
| 987 | FASTOP2W(btc); |
| 988 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 989 | FASTOP2(xadd); |
| 990 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 991 | FASTOP2R(cmp, cmp_r); |
| 992 | |
Nadav Amit | 900efe2 | 2015-03-30 15:39:21 +0300 | [diff] [blame] | 993 | static int em_bsf_c(struct x86_emulate_ctxt *ctxt) |
| 994 | { |
| 995 | /* If src is zero, do not writeback, but update flags */ |
| 996 | if (ctxt->src.val == 0) |
| 997 | ctxt->dst.type = OP_NONE; |
| 998 | return fastop(ctxt, em_bsf); |
| 999 | } |
| 1000 | |
| 1001 | static int em_bsr_c(struct x86_emulate_ctxt *ctxt) |
| 1002 | { |
| 1003 | /* If src is zero, do not writeback, but update flags */ |
| 1004 | if (ctxt->src.val == 0) |
| 1005 | ctxt->dst.type = OP_NONE; |
| 1006 | return fastop(ctxt, em_bsr); |
| 1007 | } |
| 1008 | |
Josh Poimboeuf | cb7390f | 2016-01-22 10:16:12 -0600 | [diff] [blame] | 1009 | static __always_inline u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1010 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1011 | u8 rc; |
| 1012 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1013 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1014 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 1015 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1016 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 1017 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1018 | } |
| 1019 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1020 | static void fetch_register_operand(struct operand *op) |
| 1021 | { |
| 1022 | switch (op->bytes) { |
| 1023 | case 1: |
| 1024 | op->val = *(u8 *)op->addr.reg; |
| 1025 | break; |
| 1026 | case 2: |
| 1027 | op->val = *(u16 *)op->addr.reg; |
| 1028 | break; |
| 1029 | case 4: |
| 1030 | op->val = *(u32 *)op->addr.reg; |
| 1031 | break; |
| 1032 | case 8: |
| 1033 | op->val = *(u64 *)op->addr.reg; |
| 1034 | break; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1038 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 1039 | { |
| 1040 | ctxt->ops->get_fpu(ctxt); |
| 1041 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1042 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 1043 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 1044 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 1045 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 1046 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 1047 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 1048 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 1049 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1050 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1051 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 1052 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 1053 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 1054 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 1055 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 1056 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 1057 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 1058 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1059 | #endif |
| 1060 | default: BUG(); |
| 1061 | } |
| 1062 | ctxt->ops->put_fpu(ctxt); |
| 1063 | } |
| 1064 | |
| 1065 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 1066 | int reg) |
| 1067 | { |
| 1068 | ctxt->ops->get_fpu(ctxt); |
| 1069 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1070 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 1071 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 1072 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 1073 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 1074 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 1075 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 1076 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 1077 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1078 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1079 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 1080 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1081 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1082 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1083 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1084 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1085 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1086 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1087 | #endif |
| 1088 | default: BUG(); |
| 1089 | } |
| 1090 | ctxt->ops->put_fpu(ctxt); |
| 1091 | } |
| 1092 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1093 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1094 | { |
| 1095 | ctxt->ops->get_fpu(ctxt); |
| 1096 | switch (reg) { |
| 1097 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1098 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1099 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1100 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1101 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1102 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1103 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1104 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1105 | default: BUG(); |
| 1106 | } |
| 1107 | ctxt->ops->put_fpu(ctxt); |
| 1108 | } |
| 1109 | |
| 1110 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1111 | { |
| 1112 | ctxt->ops->get_fpu(ctxt); |
| 1113 | switch (reg) { |
| 1114 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1115 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1116 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1117 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1118 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1119 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1120 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1121 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1122 | default: BUG(); |
| 1123 | } |
| 1124 | ctxt->ops->put_fpu(ctxt); |
| 1125 | } |
| 1126 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1127 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1128 | { |
| 1129 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1130 | return emulate_nm(ctxt); |
| 1131 | |
| 1132 | ctxt->ops->get_fpu(ctxt); |
| 1133 | asm volatile("fninit"); |
| 1134 | ctxt->ops->put_fpu(ctxt); |
| 1135 | return X86EMUL_CONTINUE; |
| 1136 | } |
| 1137 | |
| 1138 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1139 | { |
| 1140 | u16 fcw; |
| 1141 | |
| 1142 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1143 | return emulate_nm(ctxt); |
| 1144 | |
| 1145 | ctxt->ops->get_fpu(ctxt); |
| 1146 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1147 | ctxt->ops->put_fpu(ctxt); |
| 1148 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1149 | ctxt->dst.val = fcw; |
| 1150 | |
| 1151 | return X86EMUL_CONTINUE; |
| 1152 | } |
| 1153 | |
| 1154 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1155 | { |
| 1156 | u16 fsw; |
| 1157 | |
| 1158 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1159 | return emulate_nm(ctxt); |
| 1160 | |
| 1161 | ctxt->ops->get_fpu(ctxt); |
| 1162 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1163 | ctxt->ops->put_fpu(ctxt); |
| 1164 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1165 | ctxt->dst.val = fsw; |
| 1166 | |
| 1167 | return X86EMUL_CONTINUE; |
| 1168 | } |
| 1169 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1170 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1171 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1172 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1173 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1174 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1175 | if (!(ctxt->d & ModRM)) |
| 1176 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1177 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1178 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1179 | op->type = OP_XMM; |
| 1180 | op->bytes = 16; |
| 1181 | op->addr.xmm = reg; |
| 1182 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1183 | return; |
| 1184 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1185 | if (ctxt->d & Mmx) { |
| 1186 | reg &= 7; |
| 1187 | op->type = OP_MM; |
| 1188 | op->bytes = 8; |
| 1189 | op->addr.mm = reg; |
| 1190 | return; |
| 1191 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1192 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1193 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1194 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1195 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1196 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1197 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1198 | op->orig_val = op->val; |
| 1199 | } |
| 1200 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1201 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1202 | { |
| 1203 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1204 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1205 | } |
| 1206 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1207 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1208 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1209 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1210 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1211 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1212 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1213 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1214 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1215 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1216 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1217 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1218 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1219 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1220 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1221 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1222 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1223 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1224 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1225 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1226 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1227 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1228 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1229 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1230 | op->type = OP_XMM; |
| 1231 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1232 | op->addr.xmm = ctxt->modrm_rm; |
| 1233 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1234 | return rc; |
| 1235 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1236 | if (ctxt->d & Mmx) { |
| 1237 | op->type = OP_MM; |
| 1238 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1239 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1240 | return rc; |
| 1241 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1242 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1243 | return rc; |
| 1244 | } |
| 1245 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1246 | op->type = OP_MEM; |
| 1247 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1248 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1249 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1250 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1251 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1252 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1253 | |
| 1254 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1255 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1256 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1257 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1258 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1259 | break; |
| 1260 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1261 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1262 | break; |
| 1263 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1264 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1265 | break; |
| 1266 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1267 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1268 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1269 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1270 | break; |
| 1271 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1272 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1275 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1276 | break; |
| 1277 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1278 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1279 | break; |
| 1280 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1281 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1282 | break; |
| 1283 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1284 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1285 | break; |
| 1286 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1287 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1288 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1289 | break; |
| 1290 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1291 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1292 | break; |
| 1293 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1294 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1295 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1296 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1297 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1298 | } else { |
| 1299 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1300 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1301 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1302 | index_reg |= (sib >> 3) & 7; |
| 1303 | base_reg |= sib & 7; |
| 1304 | scale = sib >> 6; |
| 1305 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1306 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1307 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1308 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1309 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1310 | adjust_modrm_seg(ctxt, base_reg); |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 1311 | /* Increment ESP on POP [ESP] */ |
| 1312 | if ((ctxt->d & IncSP) && |
| 1313 | base_reg == VCPU_REGS_RSP) |
| 1314 | modrm_ea += ctxt->op_bytes; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1315 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1316 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1317 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1318 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Nadav Amit | 5b38ab8 | 2014-11-02 11:54:41 +0200 | [diff] [blame] | 1319 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1320 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1321 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1322 | } else { |
| 1323 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1324 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1325 | adjust_modrm_seg(ctxt, base_reg); |
| 1326 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1327 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1328 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1329 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1330 | break; |
| 1331 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1332 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1336 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1337 | if (ctxt->ad_bytes != 8) |
| 1338 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1339 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1340 | done: |
| 1341 | return rc; |
| 1342 | } |
| 1343 | |
| 1344 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1345 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1346 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1347 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1348 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1349 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1350 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1351 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1352 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1353 | break; |
| 1354 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1355 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1356 | break; |
| 1357 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1358 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1359 | break; |
| 1360 | } |
| 1361 | done: |
| 1362 | return rc; |
| 1363 | } |
| 1364 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1365 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1366 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1367 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1368 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1369 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1370 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1371 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1372 | if (ctxt->src.bytes == 2) |
| 1373 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1374 | else if (ctxt->src.bytes == 4) |
| 1375 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1376 | else |
| 1377 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1378 | |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 1379 | ctxt->dst.addr.mem.ea = address_mask(ctxt, |
| 1380 | ctxt->dst.addr.mem.ea + (sv >> 3)); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1381 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1382 | |
| 1383 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1384 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1387 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1388 | unsigned long addr, void *dest, unsigned size) |
| 1389 | { |
| 1390 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1391 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1392 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1393 | if (mc->pos < mc->end) |
| 1394 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1395 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1396 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1397 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1398 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1399 | &ctxt->exception); |
| 1400 | if (rc != X86EMUL_CONTINUE) |
| 1401 | return rc; |
| 1402 | |
| 1403 | mc->end += size; |
| 1404 | |
| 1405 | read_cached: |
| 1406 | memcpy(dest, mc->data + mc->pos, size); |
| 1407 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1408 | return X86EMUL_CONTINUE; |
| 1409 | } |
| 1410 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1411 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1412 | struct segmented_address addr, |
| 1413 | void *data, |
| 1414 | unsigned size) |
| 1415 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1416 | int rc; |
| 1417 | ulong linear; |
| 1418 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1419 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1420 | if (rc != X86EMUL_CONTINUE) |
| 1421 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1422 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1426 | struct segmented_address addr, |
| 1427 | const void *data, |
| 1428 | unsigned size) |
| 1429 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1430 | int rc; |
| 1431 | ulong linear; |
| 1432 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1433 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1434 | if (rc != X86EMUL_CONTINUE) |
| 1435 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1436 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1437 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1441 | struct segmented_address addr, |
| 1442 | const void *orig_data, const void *data, |
| 1443 | unsigned size) |
| 1444 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1445 | int rc; |
| 1446 | ulong linear; |
| 1447 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1448 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1449 | if (rc != X86EMUL_CONTINUE) |
| 1450 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1451 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1452 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1455 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1456 | unsigned int size, unsigned short port, |
| 1457 | void *dest) |
| 1458 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1459 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1460 | |
| 1461 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1462 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1463 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1464 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1465 | in_page = (ctxt->eflags & X86_EFLAGS_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1466 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1467 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Mark Rustad | b55a814 | 2014-07-25 06:27:05 -0700 | [diff] [blame] | 1468 | n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1469 | if (n == 0) |
| 1470 | n = 1; |
| 1471 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1472 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1473 | return 0; |
| 1474 | rc->end = n * size; |
| 1475 | } |
| 1476 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1477 | if (ctxt->rep_prefix && (ctxt->d & String) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1478 | !(ctxt->eflags & X86_EFLAGS_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1479 | ctxt->dst.data = rc->data + rc->pos; |
| 1480 | ctxt->dst.type = OP_MEM_STR; |
| 1481 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1482 | rc->pos = rc->end; |
| 1483 | } else { |
| 1484 | memcpy(dest, rc->data + rc->pos, size); |
| 1485 | rc->pos += size; |
| 1486 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1487 | return 1; |
| 1488 | } |
| 1489 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1490 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1491 | u16 index, struct desc_struct *desc) |
| 1492 | { |
| 1493 | struct desc_ptr dt; |
| 1494 | ulong addr; |
| 1495 | |
| 1496 | ctxt->ops->get_idt(ctxt, &dt); |
| 1497 | |
| 1498 | if (dt.size < index * 8 + 7) |
| 1499 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1500 | |
| 1501 | addr = dt.address + index * 8; |
| 1502 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1503 | &ctxt->exception); |
| 1504 | } |
| 1505 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1506 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1507 | u16 selector, struct desc_ptr *dt) |
| 1508 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1509 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1510 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1511 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1512 | if (selector & 1 << 2) { |
| 1513 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1514 | u16 sel; |
| 1515 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1516 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1517 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1518 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1519 | return; |
| 1520 | |
| 1521 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1522 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1523 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1524 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1525 | } |
| 1526 | |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1527 | static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt, |
| 1528 | u16 selector, ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1529 | { |
| 1530 | struct desc_ptr dt; |
| 1531 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1532 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1533 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1534 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1535 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1536 | if (dt.size < index * 8 + 7) |
| 1537 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1538 | |
| 1539 | addr = dt.address + index * 8; |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1540 | |
| 1541 | #ifdef CONFIG_X86_64 |
| 1542 | if (addr >> 32 != 0) { |
| 1543 | u64 efer = 0; |
| 1544 | |
| 1545 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1546 | if (!(efer & EFER_LMA)) |
| 1547 | addr &= (u32)-1; |
| 1548 | } |
| 1549 | #endif |
| 1550 | |
| 1551 | *desc_addr_p = addr; |
| 1552 | return X86EMUL_CONTINUE; |
| 1553 | } |
| 1554 | |
| 1555 | /* allowed just for 8 bytes segments */ |
| 1556 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1557 | u16 selector, struct desc_struct *desc, |
| 1558 | ulong *desc_addr_p) |
| 1559 | { |
| 1560 | int rc; |
| 1561 | |
| 1562 | rc = get_descriptor_ptr(ctxt, selector, desc_addr_p); |
| 1563 | if (rc != X86EMUL_CONTINUE) |
| 1564 | return rc; |
| 1565 | |
| 1566 | return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc), |
| 1567 | &ctxt->exception); |
| 1568 | } |
| 1569 | |
| 1570 | /* allowed just for 8 bytes segments */ |
| 1571 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1572 | u16 selector, struct desc_struct *desc) |
| 1573 | { |
| 1574 | int rc; |
| 1575 | ulong addr; |
| 1576 | |
| 1577 | rc = get_descriptor_ptr(ctxt, selector, &addr); |
| 1578 | if (rc != X86EMUL_CONTINUE) |
| 1579 | return rc; |
| 1580 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1581 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1582 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1583 | } |
| 1584 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1585 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1586 | u16 selector, int seg, u8 cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1587 | enum x86_transfer_type transfer, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1588 | struct desc_struct *desc) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1589 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1590 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1591 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1592 | unsigned err_vec = GP_VECTOR; |
| 1593 | u32 err_code = 0; |
| 1594 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1595 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1596 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1597 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1598 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1599 | |
| 1600 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1601 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1602 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1603 | /* set real mode segment descriptor (keep limit etc. for |
| 1604 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1605 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1606 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1607 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1608 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1609 | /* VM86 needs a clean new segment descriptor */ |
| 1610 | set_desc_base(&seg_desc, selector << 4); |
| 1611 | set_desc_limit(&seg_desc, 0xffff); |
| 1612 | seg_desc.type = 3; |
| 1613 | seg_desc.p = 1; |
| 1614 | seg_desc.s = 1; |
| 1615 | seg_desc.dpl = 3; |
| 1616 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1617 | } |
| 1618 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1619 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1620 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1621 | /* TR should be in GDT only */ |
| 1622 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1623 | goto exception; |
| 1624 | |
Paolo Bonzini | 7718ffc | 2017-01-12 15:02:32 +0100 | [diff] [blame] | 1625 | /* NULL selector is not valid for TR, CS and (except for long mode) SS */ |
| 1626 | if (null_selector) { |
| 1627 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR) |
| 1628 | goto exception; |
| 1629 | |
| 1630 | if (seg == VCPU_SREG_SS) { |
| 1631 | if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl) |
| 1632 | goto exception; |
| 1633 | |
| 1634 | /* |
| 1635 | * ctxt->ops->set_segment expects the CPL to be in |
| 1636 | * SS.DPL, so fake an expand-up 32-bit data segment. |
| 1637 | */ |
| 1638 | seg_desc.type = 3; |
| 1639 | seg_desc.p = 1; |
| 1640 | seg_desc.s = 1; |
| 1641 | seg_desc.dpl = cpl; |
| 1642 | seg_desc.d = 1; |
| 1643 | seg_desc.g = 1; |
| 1644 | } |
| 1645 | |
| 1646 | /* Skip all following checks */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1647 | goto load; |
Paolo Bonzini | 7718ffc | 2017-01-12 15:02:32 +0100 | [diff] [blame] | 1648 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1649 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1650 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1651 | if (ret != X86EMUL_CONTINUE) |
| 1652 | return ret; |
| 1653 | |
| 1654 | err_code = selector & 0xfffc; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1655 | err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR : |
| 1656 | GP_VECTOR; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1657 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1658 | /* can't load system descriptor into segment selector */ |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1659 | if (seg <= VCPU_SREG_GS && !seg_desc.s) { |
| 1660 | if (transfer == X86_TRANSFER_CALL_JMP) |
| 1661 | return X86EMUL_UNHANDLEABLE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1662 | goto exception; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1663 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1664 | |
| 1665 | if (!seg_desc.p) { |
| 1666 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1667 | goto exception; |
| 1668 | } |
| 1669 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1670 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1671 | |
| 1672 | switch (seg) { |
| 1673 | case VCPU_SREG_SS: |
| 1674 | /* |
| 1675 | * segment is not a writable data segment or segment |
| 1676 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1677 | */ |
| 1678 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1679 | goto exception; |
| 1680 | break; |
| 1681 | case VCPU_SREG_CS: |
| 1682 | if (!(seg_desc.type & 8)) |
| 1683 | goto exception; |
| 1684 | |
| 1685 | if (seg_desc.type & 4) { |
| 1686 | /* conforming */ |
| 1687 | if (dpl > cpl) |
| 1688 | goto exception; |
| 1689 | } else { |
| 1690 | /* nonconforming */ |
| 1691 | if (rpl > cpl || dpl != cpl) |
| 1692 | goto exception; |
| 1693 | } |
Nadav Amit | 040c8dc | 2014-09-18 22:39:43 +0300 | [diff] [blame] | 1694 | /* in long-mode d/b must be clear if l is set */ |
| 1695 | if (seg_desc.d && seg_desc.l) { |
| 1696 | u64 efer = 0; |
| 1697 | |
| 1698 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1699 | if (efer & EFER_LMA) |
| 1700 | goto exception; |
| 1701 | } |
| 1702 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1703 | /* CS(RPL) <- CPL */ |
| 1704 | selector = (selector & 0xfffc) | cpl; |
| 1705 | break; |
| 1706 | case VCPU_SREG_TR: |
| 1707 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1708 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1709 | old_desc = seg_desc; |
| 1710 | seg_desc.type |= 2; /* busy */ |
| 1711 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1712 | sizeof(seg_desc), &ctxt->exception); |
| 1713 | if (ret != X86EMUL_CONTINUE) |
| 1714 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1715 | break; |
| 1716 | case VCPU_SREG_LDTR: |
| 1717 | if (seg_desc.s || seg_desc.type != 2) |
| 1718 | goto exception; |
| 1719 | break; |
| 1720 | default: /* DS, ES, FS, or GS */ |
| 1721 | /* |
| 1722 | * segment is not a data or readable code segment or |
| 1723 | * ((segment is a data or nonconforming code segment) |
| 1724 | * and (both RPL and CPL > DPL)) |
| 1725 | */ |
| 1726 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1727 | (((seg_desc.type & 0xc) != 0xc) && |
| 1728 | (rpl > dpl && cpl > dpl))) |
| 1729 | goto exception; |
| 1730 | break; |
| 1731 | } |
| 1732 | |
| 1733 | if (seg_desc.s) { |
| 1734 | /* mark segment as accessed */ |
Nadav Amit | e2cefa7 | 2014-12-25 02:52:22 +0200 | [diff] [blame] | 1735 | if (!(seg_desc.type & 1)) { |
| 1736 | seg_desc.type |= 1; |
| 1737 | ret = write_segment_descriptor(ctxt, selector, |
| 1738 | &seg_desc); |
| 1739 | if (ret != X86EMUL_CONTINUE) |
| 1740 | return ret; |
| 1741 | } |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1742 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1743 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1744 | sizeof(base3), &ctxt->exception); |
| 1745 | if (ret != X86EMUL_CONTINUE) |
| 1746 | return ret; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 1747 | if (is_noncanonical_address(get_desc_base(&seg_desc) | |
| 1748 | ((u64)base3 << 32))) |
| 1749 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1750 | } |
| 1751 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1752 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1753 | if (desc) |
| 1754 | *desc = seg_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1755 | return X86EMUL_CONTINUE; |
| 1756 | exception: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 1757 | return emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1760 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1761 | u16 selector, int seg) |
| 1762 | { |
| 1763 | u8 cpl = ctxt->ops->cpl(ctxt); |
Paolo Bonzini | 7718ffc | 2017-01-12 15:02:32 +0100 | [diff] [blame] | 1764 | |
| 1765 | /* |
| 1766 | * None of MOV, POP and LSS can load a NULL selector in CPL=3, but |
| 1767 | * they can load it at CPL<3 (Intel's manual says only LSS can, |
| 1768 | * but it's wrong). |
| 1769 | * |
| 1770 | * However, the Intel manual says that putting IST=1/DPL=3 in |
| 1771 | * an interrupt gate will result in SS=3 (the AMD manual instead |
| 1772 | * says it doesn't), so allow SS=3 in __load_segment_descriptor |
| 1773 | * and only forbid it here. |
| 1774 | */ |
| 1775 | if (seg == VCPU_SREG_SS && selector == 3 && |
| 1776 | ctxt->mode == X86EMUL_MODE_PROT64) |
| 1777 | return emulate_exception(ctxt, GP_VECTOR, 0, true); |
| 1778 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1779 | return __load_segment_descriptor(ctxt, selector, seg, cpl, |
| 1780 | X86_TRANSFER_NONE, NULL); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1783 | static void write_register_operand(struct operand *op) |
| 1784 | { |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 1785 | return assign_register(op->addr.reg, op->val, op->bytes); |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1786 | } |
| 1787 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1788 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1789 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1790 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1791 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1792 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1793 | break; |
| 1794 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1795 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1796 | return segmented_cmpxchg(ctxt, |
| 1797 | op->addr.mem, |
| 1798 | &op->orig_val, |
| 1799 | &op->val, |
| 1800 | op->bytes); |
| 1801 | else |
| 1802 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1803 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1804 | &op->val, |
| 1805 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1806 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1807 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1808 | return segmented_write(ctxt, |
| 1809 | op->addr.mem, |
| 1810 | op->data, |
| 1811 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1812 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1813 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1814 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1815 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1816 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1817 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1818 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1819 | case OP_NONE: |
| 1820 | /* no writeback */ |
| 1821 | break; |
| 1822 | default: |
| 1823 | break; |
| 1824 | } |
| 1825 | return X86EMUL_CONTINUE; |
| 1826 | } |
| 1827 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1828 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1829 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1830 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1831 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1832 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1833 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1834 | addr.seg = VCPU_SREG_SS; |
| 1835 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1836 | return segmented_write(ctxt, addr, data, bytes); |
| 1837 | } |
| 1838 | |
| 1839 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1840 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1841 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1842 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1843 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1846 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1847 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1848 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1849 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1850 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1851 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1852 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1853 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1854 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1855 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1856 | return rc; |
| 1857 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1858 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1859 | return rc; |
| 1860 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1861 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1862 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1863 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1864 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1865 | } |
| 1866 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1867 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1868 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1869 | { |
| 1870 | int rc; |
| 1871 | unsigned long val, change_mask; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1872 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1873 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1874 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1875 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1876 | if (rc != X86EMUL_CONTINUE) |
| 1877 | return rc; |
| 1878 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1879 | change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 1880 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF | |
| 1881 | X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT | |
| 1882 | X86_EFLAGS_AC | X86_EFLAGS_ID; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1883 | |
| 1884 | switch(ctxt->mode) { |
| 1885 | case X86EMUL_MODE_PROT64: |
| 1886 | case X86EMUL_MODE_PROT32: |
| 1887 | case X86EMUL_MODE_PROT16: |
| 1888 | if (cpl == 0) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1889 | change_mask |= X86_EFLAGS_IOPL; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1890 | if (cpl <= iopl) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1891 | change_mask |= X86_EFLAGS_IF; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1892 | break; |
| 1893 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1894 | if (iopl < 3) |
| 1895 | return emulate_gp(ctxt, 0); |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1896 | change_mask |= X86_EFLAGS_IF; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1897 | break; |
| 1898 | default: /* real mode */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 1899 | change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1900 | break; |
| 1901 | } |
| 1902 | |
| 1903 | *(unsigned long *)dest = |
| 1904 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1905 | |
| 1906 | return rc; |
| 1907 | } |
| 1908 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1909 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1910 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1911 | ctxt->dst.type = OP_REG; |
| 1912 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1913 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1914 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1915 | } |
| 1916 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1917 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1918 | { |
| 1919 | int rc; |
| 1920 | unsigned frame_size = ctxt->src.val; |
| 1921 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1922 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1923 | |
| 1924 | if (nesting_level) |
| 1925 | return X86EMUL_UNHANDLEABLE; |
| 1926 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1927 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1928 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1929 | if (rc != X86EMUL_CONTINUE) |
| 1930 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1931 | 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] | 1932 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1933 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1934 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1935 | stack_mask(ctxt)); |
| 1936 | return X86EMUL_CONTINUE; |
| 1937 | } |
| 1938 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1939 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1940 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1941 | 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] | 1942 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1943 | 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] | 1944 | } |
| 1945 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1946 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1947 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1948 | int seg = ctxt->src2.val; |
| 1949 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1950 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Nadav Amit | 0fcc207 | 2014-11-02 11:54:51 +0200 | [diff] [blame] | 1951 | if (ctxt->op_bytes == 4) { |
| 1952 | rsp_increment(ctxt, -2); |
| 1953 | ctxt->op_bytes = 2; |
| 1954 | } |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1955 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1956 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1957 | } |
| 1958 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1959 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1960 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1961 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1962 | unsigned long selector; |
| 1963 | int rc; |
| 1964 | |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1965 | rc = emulate_pop(ctxt, &selector, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1966 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1967 | return rc; |
| 1968 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1969 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1970 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1971 | if (ctxt->op_bytes > 2) |
| 1972 | rsp_increment(ctxt, ctxt->op_bytes - 2); |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1973 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1974 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1975 | return rc; |
| 1976 | } |
| 1977 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1978 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1979 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1980 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1981 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1982 | int reg = VCPU_REGS_RAX; |
| 1983 | |
| 1984 | while (reg <= VCPU_REGS_RDI) { |
| 1985 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1986 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1987 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1988 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1989 | if (rc != X86EMUL_CONTINUE) |
| 1990 | return rc; |
| 1991 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1992 | ++reg; |
| 1993 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1994 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1995 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1998 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1999 | { |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2000 | ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 2001 | return em_push(ctxt); |
| 2002 | } |
| 2003 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 2004 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2005 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2006 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2007 | int reg = VCPU_REGS_RDI; |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 2008 | u32 val; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2009 | |
| 2010 | while (reg >= VCPU_REGS_RAX) { |
| 2011 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 2012 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2013 | --reg; |
| 2014 | } |
| 2015 | |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 2016 | rc = emulate_pop(ctxt, &val, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2017 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2018 | break; |
Nadav Amit | 6fd8e12 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 2019 | assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2020 | --reg; |
| 2021 | } |
| 2022 | return rc; |
| 2023 | } |
| 2024 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2025 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2026 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2027 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 2028 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2029 | struct desc_ptr dt; |
| 2030 | gva_t cs_addr; |
| 2031 | gva_t eip_addr; |
| 2032 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2033 | |
| 2034 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2035 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2036 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 2037 | if (rc != X86EMUL_CONTINUE) |
| 2038 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2039 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2040 | ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2041 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2042 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2043 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 2044 | if (rc != X86EMUL_CONTINUE) |
| 2045 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2046 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2047 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2048 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 2049 | if (rc != X86EMUL_CONTINUE) |
| 2050 | return rc; |
| 2051 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2052 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2053 | |
| 2054 | eip_addr = dt.address + (irq << 2); |
| 2055 | cs_addr = dt.address + (irq << 2) + 2; |
| 2056 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2057 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2058 | if (rc != X86EMUL_CONTINUE) |
| 2059 | return rc; |
| 2060 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2061 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2062 | if (rc != X86EMUL_CONTINUE) |
| 2063 | return rc; |
| 2064 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2065 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2066 | if (rc != X86EMUL_CONTINUE) |
| 2067 | return rc; |
| 2068 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2069 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2070 | |
| 2071 | return rc; |
| 2072 | } |
| 2073 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2074 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 2075 | { |
| 2076 | int rc; |
| 2077 | |
| 2078 | invalidate_registers(ctxt); |
| 2079 | rc = __emulate_int_real(ctxt, irq); |
| 2080 | if (rc == X86EMUL_CONTINUE) |
| 2081 | writeback_registers(ctxt); |
| 2082 | return rc; |
| 2083 | } |
| 2084 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2085 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2086 | { |
| 2087 | switch(ctxt->mode) { |
| 2088 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2089 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2090 | case X86EMUL_MODE_VM86: |
| 2091 | case X86EMUL_MODE_PROT16: |
| 2092 | case X86EMUL_MODE_PROT32: |
| 2093 | case X86EMUL_MODE_PROT64: |
| 2094 | default: |
| 2095 | /* Protected mode interrupts unimplemented yet */ |
| 2096 | return X86EMUL_UNHANDLEABLE; |
| 2097 | } |
| 2098 | } |
| 2099 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2100 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2101 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2102 | int rc = X86EMUL_CONTINUE; |
| 2103 | unsigned long temp_eip = 0; |
| 2104 | unsigned long temp_eflags = 0; |
| 2105 | unsigned long cs = 0; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2106 | unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 2107 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF | |
| 2108 | X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF | |
| 2109 | X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF | |
| 2110 | X86_EFLAGS_AC | X86_EFLAGS_ID | |
Wanpeng Li | 35fd68a | 2015-04-08 14:08:14 +0800 | [diff] [blame] | 2111 | X86_EFLAGS_FIXED; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2112 | unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF | |
| 2113 | X86_EFLAGS_VIP; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2114 | |
| 2115 | /* TODO: Add stack limit check */ |
| 2116 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2117 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2118 | |
| 2119 | if (rc != X86EMUL_CONTINUE) |
| 2120 | return rc; |
| 2121 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2122 | if (temp_eip & ~0xffff) |
| 2123 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2124 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2125 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2126 | |
| 2127 | if (rc != X86EMUL_CONTINUE) |
| 2128 | return rc; |
| 2129 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2130 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2131 | |
| 2132 | if (rc != X86EMUL_CONTINUE) |
| 2133 | return rc; |
| 2134 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2135 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2136 | |
| 2137 | if (rc != X86EMUL_CONTINUE) |
| 2138 | return rc; |
| 2139 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2140 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2141 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2142 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2143 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2144 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2145 | ctxt->eflags &= ~0xffff; |
| 2146 | ctxt->eflags |= temp_eflags; |
| 2147 | } |
| 2148 | |
| 2149 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
Wanpeng Li | 35fd68a | 2015-04-08 14:08:14 +0800 | [diff] [blame] | 2150 | ctxt->eflags |= X86_EFLAGS_FIXED; |
Nadav Amit | 801806d | 2015-01-26 09:32:23 +0200 | [diff] [blame] | 2151 | ctxt->ops->set_nmi_mask(ctxt, false); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2152 | |
| 2153 | return rc; |
| 2154 | } |
| 2155 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2156 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2157 | { |
| 2158 | switch(ctxt->mode) { |
| 2159 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2160 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2161 | case X86EMUL_MODE_VM86: |
| 2162 | case X86EMUL_MODE_PROT16: |
| 2163 | case X86EMUL_MODE_PROT32: |
| 2164 | case X86EMUL_MODE_PROT64: |
| 2165 | default: |
| 2166 | /* iret from protected mode unimplemented yet */ |
| 2167 | return X86EMUL_UNHANDLEABLE; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2171 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2172 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2173 | int rc; |
Radim Krčmář | 2117d53 | 2016-11-23 21:15:00 +0100 | [diff] [blame] | 2174 | unsigned short sel; |
| 2175 | struct desc_struct new_desc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2176 | u8 cpl = ctxt->ops->cpl(ctxt); |
| 2177 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2178 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2179 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2180 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 2181 | X86_TRANSFER_CALL_JMP, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2182 | &new_desc); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2183 | if (rc != X86EMUL_CONTINUE) |
| 2184 | return rc; |
| 2185 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2186 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Radim Krčmář | 2117d53 | 2016-11-23 21:15:00 +0100 | [diff] [blame] | 2187 | /* Error handling is not implemented. */ |
| 2188 | if (rc != X86EMUL_CONTINUE) |
| 2189 | return X86EMUL_UNHANDLEABLE; |
| 2190 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2191 | return rc; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2192 | } |
| 2193 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2194 | static int em_jmp_abs(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2195 | { |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2196 | return assign_eip_near(ctxt, ctxt->src.val); |
| 2197 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2198 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2199 | static int em_call_near_abs(struct x86_emulate_ctxt *ctxt) |
| 2200 | { |
| 2201 | int rc; |
| 2202 | long int old_eip; |
| 2203 | |
| 2204 | old_eip = ctxt->_eip; |
| 2205 | rc = assign_eip_near(ctxt, ctxt->src.val); |
| 2206 | if (rc != X86EMUL_CONTINUE) |
| 2207 | return rc; |
| 2208 | ctxt->src.val = old_eip; |
| 2209 | rc = em_push(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2210 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2211 | } |
| 2212 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2213 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2214 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2215 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2216 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2217 | if (ctxt->dst.bytes == 16) |
| 2218 | return X86EMUL_UNHANDLEABLE; |
| 2219 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2220 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2221 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2222 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2223 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2224 | ctxt->eflags &= ~X86_EFLAGS_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2225 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2226 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2227 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2228 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2229 | ctxt->eflags |= X86_EFLAGS_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2230 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2231 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2234 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2235 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2236 | int rc; |
| 2237 | unsigned long eip; |
| 2238 | |
| 2239 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 2240 | if (rc != X86EMUL_CONTINUE) |
| 2241 | return rc; |
| 2242 | |
| 2243 | return assign_eip_near(ctxt, eip); |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2244 | } |
| 2245 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2246 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2247 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2248 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2249 | unsigned long eip, cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2250 | int cpl = ctxt->ops->cpl(ctxt); |
Radim Krčmář | 2117d53 | 2016-11-23 21:15:00 +0100 | [diff] [blame] | 2251 | struct desc_struct new_desc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2252 | |
| 2253 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2254 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2255 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2256 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2257 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2258 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2259 | /* Outer-privilege level return is not implemented */ |
| 2260 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2261 | return X86EMUL_UNHANDLEABLE; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2262 | rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, |
| 2263 | X86_TRANSFER_RET, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2264 | &new_desc); |
| 2265 | if (rc != X86EMUL_CONTINUE) |
| 2266 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2267 | rc = assign_eip_far(ctxt, eip, &new_desc); |
Radim Krčmář | 2117d53 | 2016-11-23 21:15:00 +0100 | [diff] [blame] | 2268 | /* Error handling is not implemented. */ |
| 2269 | if (rc != X86EMUL_CONTINUE) |
| 2270 | return X86EMUL_UNHANDLEABLE; |
| 2271 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2272 | return rc; |
| 2273 | } |
| 2274 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2275 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2276 | { |
| 2277 | int rc; |
| 2278 | |
| 2279 | rc = em_ret_far(ctxt); |
| 2280 | if (rc != X86EMUL_CONTINUE) |
| 2281 | return rc; |
| 2282 | rsp_increment(ctxt, ctxt->src.val); |
| 2283 | return X86EMUL_CONTINUE; |
| 2284 | } |
| 2285 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2286 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2287 | { |
| 2288 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2289 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2290 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2291 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2292 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2293 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2294 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2295 | if (ctxt->eflags & X86_EFLAGS_ZF) { |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2296 | /* Success: write back to memory; no update of EAX */ |
| 2297 | ctxt->src.type = OP_NONE; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2298 | ctxt->dst.val = ctxt->src.orig_val; |
| 2299 | } else { |
| 2300 | /* Failure: write the value we saw to EAX. */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2301 | ctxt->src.type = OP_REG; |
| 2302 | ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 2303 | ctxt->src.val = ctxt->dst.orig_val; |
| 2304 | /* Create write-cycle to dest by writing the same value */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2305 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2306 | } |
| 2307 | return X86EMUL_CONTINUE; |
| 2308 | } |
| 2309 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2310 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2311 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2312 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2313 | unsigned short sel; |
| 2314 | int rc; |
| 2315 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2316 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2317 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2318 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2319 | if (rc != X86EMUL_CONTINUE) |
| 2320 | return rc; |
| 2321 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2322 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2323 | return rc; |
| 2324 | } |
| 2325 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2326 | static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt) |
| 2327 | { |
| 2328 | u32 eax, ebx, ecx, edx; |
| 2329 | |
| 2330 | eax = 0x80000001; |
| 2331 | ecx = 0; |
| 2332 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2333 | return edx & bit(X86_FEATURE_LM); |
| 2334 | } |
| 2335 | |
| 2336 | #define GET_SMSTATE(type, smbase, offset) \ |
| 2337 | ({ \ |
| 2338 | type __val; \ |
Radim Krčmář | f40606b | 2015-10-30 16:36:25 +0100 | [diff] [blame] | 2339 | int r = ctxt->ops->read_phys(ctxt, smbase + offset, &__val, \ |
| 2340 | sizeof(__val)); \ |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2341 | if (r != X86EMUL_CONTINUE) \ |
| 2342 | return X86EMUL_UNHANDLEABLE; \ |
| 2343 | __val; \ |
| 2344 | }) |
| 2345 | |
| 2346 | static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags) |
| 2347 | { |
| 2348 | desc->g = (flags >> 23) & 1; |
| 2349 | desc->d = (flags >> 22) & 1; |
| 2350 | desc->l = (flags >> 21) & 1; |
| 2351 | desc->avl = (flags >> 20) & 1; |
| 2352 | desc->p = (flags >> 15) & 1; |
| 2353 | desc->dpl = (flags >> 13) & 3; |
| 2354 | desc->s = (flags >> 12) & 1; |
| 2355 | desc->type = (flags >> 8) & 15; |
| 2356 | } |
| 2357 | |
| 2358 | static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, u64 smbase, int n) |
| 2359 | { |
| 2360 | struct desc_struct desc; |
| 2361 | int offset; |
| 2362 | u16 selector; |
| 2363 | |
| 2364 | selector = GET_SMSTATE(u32, smbase, 0x7fa8 + n * 4); |
| 2365 | |
| 2366 | if (n < 3) |
| 2367 | offset = 0x7f84 + n * 12; |
| 2368 | else |
| 2369 | offset = 0x7f2c + (n - 3) * 12; |
| 2370 | |
| 2371 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8)); |
| 2372 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4)); |
| 2373 | rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, offset)); |
| 2374 | ctxt->ops->set_segment(ctxt, selector, &desc, 0, n); |
| 2375 | return X86EMUL_CONTINUE; |
| 2376 | } |
| 2377 | |
| 2378 | static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, u64 smbase, int n) |
| 2379 | { |
| 2380 | struct desc_struct desc; |
| 2381 | int offset; |
| 2382 | u16 selector; |
| 2383 | u32 base3; |
| 2384 | |
| 2385 | offset = 0x7e00 + n * 16; |
| 2386 | |
| 2387 | selector = GET_SMSTATE(u16, smbase, offset); |
| 2388 | rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smbase, offset + 2) << 8); |
| 2389 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4)); |
| 2390 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8)); |
| 2391 | base3 = GET_SMSTATE(u32, smbase, offset + 12); |
| 2392 | |
| 2393 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, n); |
| 2394 | return X86EMUL_CONTINUE; |
| 2395 | } |
| 2396 | |
| 2397 | static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt, |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2398 | u64 cr0, u64 cr3, u64 cr4) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2399 | { |
| 2400 | int bad; |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2401 | u64 pcid; |
| 2402 | |
| 2403 | /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */ |
| 2404 | pcid = 0; |
| 2405 | if (cr4 & X86_CR4_PCIDE) { |
| 2406 | pcid = cr3 & 0xfff; |
| 2407 | cr3 &= ~0xfff; |
| 2408 | } |
| 2409 | |
| 2410 | bad = ctxt->ops->set_cr(ctxt, 3, cr3); |
| 2411 | if (bad) |
| 2412 | return X86EMUL_UNHANDLEABLE; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2413 | |
| 2414 | /* |
| 2415 | * First enable PAE, long mode needs it before CR0.PG = 1 is set. |
| 2416 | * Then enable protected mode. However, PCID cannot be enabled |
| 2417 | * if EFER.LMA=0, so set it separately. |
| 2418 | */ |
| 2419 | bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE); |
| 2420 | if (bad) |
| 2421 | return X86EMUL_UNHANDLEABLE; |
| 2422 | |
| 2423 | bad = ctxt->ops->set_cr(ctxt, 0, cr0); |
| 2424 | if (bad) |
| 2425 | return X86EMUL_UNHANDLEABLE; |
| 2426 | |
| 2427 | if (cr4 & X86_CR4_PCIDE) { |
| 2428 | bad = ctxt->ops->set_cr(ctxt, 4, cr4); |
| 2429 | if (bad) |
| 2430 | return X86EMUL_UNHANDLEABLE; |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2431 | if (pcid) { |
| 2432 | bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid); |
| 2433 | if (bad) |
| 2434 | return X86EMUL_UNHANDLEABLE; |
| 2435 | } |
| 2436 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | return X86EMUL_CONTINUE; |
| 2440 | } |
| 2441 | |
| 2442 | static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt, u64 smbase) |
| 2443 | { |
| 2444 | struct desc_struct desc; |
| 2445 | struct desc_ptr dt; |
| 2446 | u16 selector; |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2447 | u32 val, cr0, cr3, cr4; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2448 | int i; |
| 2449 | |
| 2450 | cr0 = GET_SMSTATE(u32, smbase, 0x7ffc); |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2451 | cr3 = GET_SMSTATE(u32, smbase, 0x7ff8); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2452 | ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7ff4) | X86_EFLAGS_FIXED; |
| 2453 | ctxt->_eip = GET_SMSTATE(u32, smbase, 0x7ff0); |
| 2454 | |
| 2455 | for (i = 0; i < 8; i++) |
| 2456 | *reg_write(ctxt, i) = GET_SMSTATE(u32, smbase, 0x7fd0 + i * 4); |
| 2457 | |
| 2458 | val = GET_SMSTATE(u32, smbase, 0x7fcc); |
| 2459 | ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1); |
| 2460 | val = GET_SMSTATE(u32, smbase, 0x7fc8); |
| 2461 | ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1); |
| 2462 | |
| 2463 | selector = GET_SMSTATE(u32, smbase, 0x7fc4); |
| 2464 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f64)); |
| 2465 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f60)); |
| 2466 | rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f5c)); |
| 2467 | ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR); |
| 2468 | |
| 2469 | selector = GET_SMSTATE(u32, smbase, 0x7fc0); |
| 2470 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f80)); |
| 2471 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f7c)); |
| 2472 | rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f78)); |
| 2473 | ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR); |
| 2474 | |
| 2475 | dt.address = GET_SMSTATE(u32, smbase, 0x7f74); |
| 2476 | dt.size = GET_SMSTATE(u32, smbase, 0x7f70); |
| 2477 | ctxt->ops->set_gdt(ctxt, &dt); |
| 2478 | |
| 2479 | dt.address = GET_SMSTATE(u32, smbase, 0x7f58); |
| 2480 | dt.size = GET_SMSTATE(u32, smbase, 0x7f54); |
| 2481 | ctxt->ops->set_idt(ctxt, &dt); |
| 2482 | |
| 2483 | for (i = 0; i < 6; i++) { |
| 2484 | int r = rsm_load_seg_32(ctxt, smbase, i); |
| 2485 | if (r != X86EMUL_CONTINUE) |
| 2486 | return r; |
| 2487 | } |
| 2488 | |
| 2489 | cr4 = GET_SMSTATE(u32, smbase, 0x7f14); |
| 2490 | |
| 2491 | ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7ef8)); |
| 2492 | |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2493 | return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, u64 smbase) |
| 2497 | { |
| 2498 | struct desc_struct desc; |
| 2499 | struct desc_ptr dt; |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2500 | u64 val, cr0, cr3, cr4; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2501 | u32 base3; |
| 2502 | u16 selector; |
Paolo Bonzini | b10d92a | 2015-10-14 15:25:52 +0200 | [diff] [blame] | 2503 | int i, r; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2504 | |
| 2505 | for (i = 0; i < 16; i++) |
| 2506 | *reg_write(ctxt, i) = GET_SMSTATE(u64, smbase, 0x7ff8 - i * 8); |
| 2507 | |
| 2508 | ctxt->_eip = GET_SMSTATE(u64, smbase, 0x7f78); |
| 2509 | ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7f70) | X86_EFLAGS_FIXED; |
| 2510 | |
| 2511 | val = GET_SMSTATE(u32, smbase, 0x7f68); |
| 2512 | ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1); |
| 2513 | val = GET_SMSTATE(u32, smbase, 0x7f60); |
| 2514 | ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1); |
| 2515 | |
| 2516 | cr0 = GET_SMSTATE(u64, smbase, 0x7f58); |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2517 | cr3 = GET_SMSTATE(u64, smbase, 0x7f50); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2518 | cr4 = GET_SMSTATE(u64, smbase, 0x7f48); |
| 2519 | ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7f00)); |
| 2520 | val = GET_SMSTATE(u64, smbase, 0x7ed0); |
| 2521 | ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA); |
| 2522 | |
| 2523 | selector = GET_SMSTATE(u32, smbase, 0x7e90); |
| 2524 | rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e92) << 8); |
| 2525 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e94)); |
| 2526 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e98)); |
| 2527 | base3 = GET_SMSTATE(u32, smbase, 0x7e9c); |
| 2528 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR); |
| 2529 | |
| 2530 | dt.size = GET_SMSTATE(u32, smbase, 0x7e84); |
| 2531 | dt.address = GET_SMSTATE(u64, smbase, 0x7e88); |
| 2532 | ctxt->ops->set_idt(ctxt, &dt); |
| 2533 | |
| 2534 | selector = GET_SMSTATE(u32, smbase, 0x7e70); |
| 2535 | rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e72) << 8); |
| 2536 | set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e74)); |
| 2537 | set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e78)); |
| 2538 | base3 = GET_SMSTATE(u32, smbase, 0x7e7c); |
| 2539 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR); |
| 2540 | |
| 2541 | dt.size = GET_SMSTATE(u32, smbase, 0x7e64); |
| 2542 | dt.address = GET_SMSTATE(u64, smbase, 0x7e68); |
| 2543 | ctxt->ops->set_gdt(ctxt, &dt); |
| 2544 | |
Paolo Bonzini | 18276e9 | 2017-12-21 00:49:14 +0100 | [diff] [blame] | 2545 | r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4); |
Paolo Bonzini | b10d92a | 2015-10-14 15:25:52 +0200 | [diff] [blame] | 2546 | if (r != X86EMUL_CONTINUE) |
| 2547 | return r; |
| 2548 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2549 | for (i = 0; i < 6; i++) { |
Paolo Bonzini | b10d92a | 2015-10-14 15:25:52 +0200 | [diff] [blame] | 2550 | r = rsm_load_seg_64(ctxt, smbase, i); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2551 | if (r != X86EMUL_CONTINUE) |
| 2552 | return r; |
| 2553 | } |
| 2554 | |
Paolo Bonzini | b10d92a | 2015-10-14 15:25:52 +0200 | [diff] [blame] | 2555 | return X86EMUL_CONTINUE; |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2556 | } |
| 2557 | |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 2558 | static int em_rsm(struct x86_emulate_ctxt *ctxt) |
| 2559 | { |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2560 | unsigned long cr0, cr4, efer; |
| 2561 | u64 smbase; |
| 2562 | int ret; |
| 2563 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 2564 | if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0) |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 2565 | return emulate_ud(ctxt); |
| 2566 | |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2567 | /* |
| 2568 | * Get back to real mode, to prepare a safe state in which to load |
Paolo Bonzini | 89651a3 | 2015-11-03 13:43:05 +0100 | [diff] [blame] | 2569 | * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU |
| 2570 | * supports long mode. |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2571 | */ |
Paolo Bonzini | 89651a3 | 2015-11-03 13:43:05 +0100 | [diff] [blame] | 2572 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 2573 | if (emulator_has_longmode(ctxt)) { |
| 2574 | struct desc_struct cs_desc; |
| 2575 | |
| 2576 | /* Zero CR4.PCIDE before CR0.PG. */ |
| 2577 | if (cr4 & X86_CR4_PCIDE) { |
| 2578 | ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE); |
| 2579 | cr4 &= ~X86_CR4_PCIDE; |
| 2580 | } |
| 2581 | |
| 2582 | /* A 32-bit code segment is required to clear EFER.LMA. */ |
| 2583 | memset(&cs_desc, 0, sizeof(cs_desc)); |
| 2584 | cs_desc.type = 0xb; |
| 2585 | cs_desc.s = cs_desc.g = cs_desc.p = 1; |
| 2586 | ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS); |
| 2587 | } |
| 2588 | |
| 2589 | /* For the 64-bit case, this will clear EFER.LMA. */ |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2590 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 2591 | if (cr0 & X86_CR0_PE) |
| 2592 | ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE)); |
Paolo Bonzini | 89651a3 | 2015-11-03 13:43:05 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* Now clear CR4.PAE (which must be done before clearing EFER.LME). */ |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2595 | if (cr4 & X86_CR4_PAE) |
| 2596 | ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE); |
Paolo Bonzini | 89651a3 | 2015-11-03 13:43:05 +0100 | [diff] [blame] | 2597 | |
| 2598 | /* And finally go back to 32-bit mode. */ |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2599 | efer = 0; |
| 2600 | ctxt->ops->set_msr(ctxt, MSR_EFER, efer); |
| 2601 | |
| 2602 | smbase = ctxt->ops->get_smbase(ctxt); |
| 2603 | if (emulator_has_longmode(ctxt)) |
| 2604 | ret = rsm_load_state_64(ctxt, smbase + 0x8000); |
| 2605 | else |
| 2606 | ret = rsm_load_state_32(ctxt, smbase + 0x8000); |
| 2607 | |
| 2608 | if (ret != X86EMUL_CONTINUE) { |
| 2609 | /* FIXME: should triple fault */ |
| 2610 | return X86EMUL_UNHANDLEABLE; |
| 2611 | } |
| 2612 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 2613 | if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0) |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2614 | ctxt->ops->set_nmi_mask(ctxt, false); |
| 2615 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 2616 | ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) & |
| 2617 | ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK)); |
Paolo Bonzini | 660a5d5 | 2015-05-05 11:50:23 +0200 | [diff] [blame] | 2618 | return X86EMUL_CONTINUE; |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2621 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2622 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2623 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2624 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2625 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2626 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2627 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2628 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2629 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2630 | cs->s = 1; |
| 2631 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2632 | cs->p = 1; |
| 2633 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2634 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2635 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2636 | set_desc_base(ss, 0); /* flat segment */ |
| 2637 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2638 | ss->g = 1; /* 4kb granularity */ |
| 2639 | ss->s = 1; |
| 2640 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2641 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2642 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2643 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2644 | ss->l = 0; |
| 2645 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2646 | } |
| 2647 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2648 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2649 | { |
| 2650 | u32 eax, ebx, ecx, edx; |
| 2651 | |
| 2652 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2653 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2654 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2655 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2656 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2657 | } |
| 2658 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2659 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2660 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2661 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2662 | u32 eax, ebx, ecx, edx; |
| 2663 | |
| 2664 | /* |
| 2665 | * syscall should always be enabled in longmode - so only become |
| 2666 | * vendor specific (cpuid) if other modes are active... |
| 2667 | */ |
| 2668 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2669 | return true; |
| 2670 | |
| 2671 | eax = 0x00000000; |
| 2672 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2673 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2674 | /* |
| 2675 | * Intel ("GenuineIntel") |
| 2676 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2677 | * longmode. Also an 64bit guest with a |
| 2678 | * 32bit compat-app running will #UD !! While this |
| 2679 | * behaviour can be fixed (by emulating) into AMD |
| 2680 | * response - CPUs of AMD can't behave like Intel. |
| 2681 | */ |
| 2682 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2683 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2684 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2685 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2686 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2687 | /* AMD ("AuthenticAMD") */ |
| 2688 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2689 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2690 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2691 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2692 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2693 | /* AMD ("AMDisbetter!") */ |
| 2694 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2695 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2696 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2697 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2698 | |
| 2699 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2700 | return false; |
| 2701 | } |
| 2702 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2703 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2704 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2705 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2706 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2707 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2708 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2709 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2710 | |
| 2711 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2712 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2713 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2714 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2715 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2716 | if (!(em_syscall_is_enabled(ctxt))) |
| 2717 | return emulate_ud(ctxt); |
| 2718 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2719 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2720 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2721 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2722 | if (!(efer & EFER_SCE)) |
| 2723 | return emulate_ud(ctxt); |
| 2724 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2725 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2726 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2727 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2728 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2729 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2730 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2731 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2732 | cs.l = 1; |
| 2733 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2734 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2735 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2736 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2737 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2738 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2739 | #ifdef CONFIG_X86_64 |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2740 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2741 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2742 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2743 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2744 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2745 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2746 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2747 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2748 | ctxt->eflags &= ~msr_data; |
Wanpeng Li | 35fd68a | 2015-04-08 14:08:14 +0800 | [diff] [blame] | 2749 | ctxt->eflags |= X86_EFLAGS_FIXED; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2750 | #endif |
| 2751 | } else { |
| 2752 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2753 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2754 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2755 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2756 | ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2757 | } |
| 2758 | |
Paolo Bonzini | 922e562 | 2017-06-07 15:13:14 +0200 | [diff] [blame] | 2759 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2760 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2761 | } |
| 2762 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2763 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2764 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2765 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2766 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2767 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2768 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2769 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2770 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2771 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2772 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2773 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2774 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2775 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2776 | /* |
| 2777 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2778 | * mode). |
| 2779 | */ |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2780 | if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA) |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2781 | && !vendor_intel(ctxt)) |
| 2782 | return emulate_ud(ctxt); |
| 2783 | |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2784 | /* sysenter/sysexit have not been tested in 64bit mode. */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2785 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2786 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2787 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2788 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2789 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2790 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2791 | if ((msr_data & 0xfffc) == 0x0) |
| 2792 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2793 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2794 | ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2795 | cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2796 | ss_sel = cs_sel + 8; |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2797 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2798 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2799 | cs.l = 1; |
| 2800 | } |
| 2801 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2802 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2803 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2804 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2805 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2806 | ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2807 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2808 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Nadav Amit | f374737 | 2015-01-01 23:11:11 +0200 | [diff] [blame] | 2809 | *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data : |
| 2810 | (u32)msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2811 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2812 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2813 | } |
| 2814 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2815 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2816 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2817 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2818 | struct desc_struct cs, ss; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2819 | u64 msr_data, rcx, rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2820 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2821 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2822 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2823 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2824 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2825 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2826 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2827 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2828 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2829 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2830 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2831 | usermode = X86EMUL_MODE_PROT64; |
| 2832 | else |
| 2833 | usermode = X86EMUL_MODE_PROT32; |
| 2834 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2835 | rcx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2836 | rdx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2837 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2838 | cs.dpl = 3; |
| 2839 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2840 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2841 | switch (usermode) { |
| 2842 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2843 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2844 | if ((msr_data & 0xfffc) == 0x0) |
| 2845 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2846 | ss_sel = (u16)(msr_data + 24); |
Nadav Amit | bf0b682 | 2014-09-18 22:39:45 +0300 | [diff] [blame] | 2847 | rcx = (u32)rcx; |
| 2848 | rdx = (u32)rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2849 | break; |
| 2850 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2851 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2852 | if (msr_data == 0x0) |
| 2853 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2854 | ss_sel = cs_sel + 8; |
| 2855 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2856 | cs.l = 1; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2857 | if (is_noncanonical_address(rcx) || |
| 2858 | is_noncanonical_address(rdx)) |
| 2859 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2860 | break; |
| 2861 | } |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2862 | cs_sel |= SEGMENT_RPL_MASK; |
| 2863 | ss_sel |= SEGMENT_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2864 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2865 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2866 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2867 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2868 | ctxt->_eip = rdx; |
| 2869 | *reg_write(ctxt, VCPU_REGS_RSP) = rcx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2870 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2871 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2872 | } |
| 2873 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2874 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2875 | { |
| 2876 | int iopl; |
| 2877 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2878 | return false; |
| 2879 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2880 | return true; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 2881 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2882 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2886 | u16 port, u16 len) |
| 2887 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2888 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2889 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2890 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2891 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2892 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2893 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2894 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2895 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2896 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2897 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2898 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2899 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2900 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2901 | base = get_desc_base(&tr_seg); |
| 2902 | #ifdef CONFIG_X86_64 |
| 2903 | base |= ((u64)base3) << 32; |
| 2904 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2905 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2906 | if (r != X86EMUL_CONTINUE) |
| 2907 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2908 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2909 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2910 | 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] | 2911 | if (r != X86EMUL_CONTINUE) |
| 2912 | return false; |
| 2913 | if ((perm >> bit_idx) & mask) |
| 2914 | return false; |
| 2915 | return true; |
| 2916 | } |
| 2917 | |
| 2918 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2919 | u16 port, u16 len) |
| 2920 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2921 | if (ctxt->perm_ok) |
| 2922 | return true; |
| 2923 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2924 | if (emulator_bad_iopl(ctxt)) |
| 2925 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2926 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2927 | |
| 2928 | ctxt->perm_ok = true; |
| 2929 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2930 | return true; |
| 2931 | } |
| 2932 | |
Nadav Amit | 428e3d0 | 2015-04-28 13:06:01 +0300 | [diff] [blame] | 2933 | static void string_registers_quirk(struct x86_emulate_ctxt *ctxt) |
| 2934 | { |
| 2935 | /* |
| 2936 | * Intel CPUs mask the counter and pointers in quite strange |
| 2937 | * manner when ECX is zero due to REP-string optimizations. |
| 2938 | */ |
| 2939 | #ifdef CONFIG_X86_64 |
| 2940 | if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt)) |
| 2941 | return; |
| 2942 | |
| 2943 | *reg_write(ctxt, VCPU_REGS_RCX) = 0; |
| 2944 | |
| 2945 | switch (ctxt->b) { |
| 2946 | case 0xa4: /* movsb */ |
| 2947 | case 0xa5: /* movsd/w */ |
| 2948 | *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1; |
| 2949 | /* fall through */ |
| 2950 | case 0xaa: /* stosb */ |
| 2951 | case 0xab: /* stosd/w */ |
| 2952 | *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1; |
| 2953 | } |
| 2954 | #endif |
| 2955 | } |
| 2956 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2957 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2958 | struct tss_segment_16 *tss) |
| 2959 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2960 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2961 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2962 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2963 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2964 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2965 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2966 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2967 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2968 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2969 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2970 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2971 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2972 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2973 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2974 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2975 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2979 | struct tss_segment_16 *tss) |
| 2980 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2981 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2982 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2983 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2984 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2985 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2986 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2987 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2988 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2989 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2990 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2991 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2992 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2993 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2994 | |
| 2995 | /* |
| 2996 | * SDM says that segment selectors are loaded before segment |
| 2997 | * descriptors |
| 2998 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2999 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 3000 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 3001 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 3002 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 3003 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3004 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3005 | cpl = tss->cs & 3; |
| 3006 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3007 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 3008 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3009 | * it is handled in a context of new task |
| 3010 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3011 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3012 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3013 | if (ret != X86EMUL_CONTINUE) |
| 3014 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3015 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3016 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3017 | if (ret != X86EMUL_CONTINUE) |
| 3018 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3019 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3020 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3021 | if (ret != X86EMUL_CONTINUE) |
| 3022 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3023 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3024 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3025 | if (ret != X86EMUL_CONTINUE) |
| 3026 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3027 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3028 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3029 | if (ret != X86EMUL_CONTINUE) |
| 3030 | return ret; |
| 3031 | |
| 3032 | return X86EMUL_CONTINUE; |
| 3033 | } |
| 3034 | |
| 3035 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3036 | u16 tss_selector, u16 old_tss_sel, |
| 3037 | ulong old_tss_base, struct desc_struct *new_desc) |
| 3038 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 3039 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3040 | struct tss_segment_16 tss_seg; |
| 3041 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 3042 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3043 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3044 | 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] | 3045 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3046 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3047 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3048 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3049 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3050 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3051 | 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] | 3052 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3053 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3054 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3055 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3056 | 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] | 3057 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3058 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3059 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3060 | |
| 3061 | if (old_tss_sel != 0xffff) { |
| 3062 | tss_seg.prev_task_link = old_tss_sel; |
| 3063 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3064 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3065 | &tss_seg.prev_task_link, |
| 3066 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3067 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3068 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3069 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3070 | } |
| 3071 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3072 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3076 | struct tss_segment_32 *tss) |
| 3077 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 3078 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3079 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3080 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3081 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3082 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 3083 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 3084 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 3085 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 3086 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 3087 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 3088 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3089 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 3090 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 3091 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 3092 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 3093 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 3094 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 3095 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3099 | struct tss_segment_32 *tss) |
| 3100 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3101 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3102 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3103 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3104 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 3105 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3106 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3107 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3110 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 3111 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 3112 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 3113 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 3114 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 3115 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 3116 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 3117 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3118 | |
| 3119 | /* |
| 3120 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3121 | * descriptors. This is important because CPL checks will |
| 3122 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3123 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 3124 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 3125 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 3126 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 3127 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 3128 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 3129 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 3130 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3131 | |
| 3132 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3133 | * If we're switching between Protected Mode and VM86, we need to make |
| 3134 | * sure to update the mode before loading the segment descriptors so |
| 3135 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3136 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3137 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3138 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3139 | cpl = 3; |
| 3140 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3141 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 3142 | cpl = tss->cs & 3; |
| 3143 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 3144 | |
| 3145 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3146 | * Now load segment descriptors. If fault happenes at this stage |
| 3147 | * it is handled in a context of new task |
| 3148 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3149 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3150 | cpl, X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3151 | if (ret != X86EMUL_CONTINUE) |
| 3152 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3153 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3154 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3155 | if (ret != X86EMUL_CONTINUE) |
| 3156 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3157 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3158 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3159 | if (ret != X86EMUL_CONTINUE) |
| 3160 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3161 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3162 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3163 | if (ret != X86EMUL_CONTINUE) |
| 3164 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3165 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3166 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3167 | if (ret != X86EMUL_CONTINUE) |
| 3168 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3169 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3170 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3171 | if (ret != X86EMUL_CONTINUE) |
| 3172 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3173 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3174 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3175 | |
Eugene Korenevsky | 2f729b1 | 2015-03-29 01:27:17 +0300 | [diff] [blame] | 3176 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3180 | u16 tss_selector, u16 old_tss_sel, |
| 3181 | ulong old_tss_base, struct desc_struct *new_desc) |
| 3182 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 3183 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3184 | struct tss_segment_32 tss_seg; |
| 3185 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 3186 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 3187 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 3188 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3189 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3190 | 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] | 3191 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3192 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3193 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3194 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3195 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3196 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 3197 | /* Only GP registers and segment selectors are saved */ |
| 3198 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 3199 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3200 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3201 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3202 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3203 | 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] | 3204 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3205 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3206 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3207 | |
| 3208 | if (old_tss_sel != 0xffff) { |
| 3209 | tss_seg.prev_task_link = old_tss_sel; |
| 3210 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3211 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3212 | &tss_seg.prev_task_link, |
| 3213 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3214 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 3215 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3216 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3219 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3223 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3224 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3225 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 3226 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3227 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 3228 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 3229 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3230 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3231 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 3232 | u32 desc_limit; |
Nadav Amit | 3db176d | 2015-04-19 21:12:59 +0300 | [diff] [blame] | 3233 | ulong desc_addr, dr7; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3234 | |
| 3235 | /* FIXME: old_tss_base == ~0 ? */ |
| 3236 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 3237 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3238 | if (ret != X86EMUL_CONTINUE) |
| 3239 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 3240 | 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] | 3241 | if (ret != X86EMUL_CONTINUE) |
| 3242 | return ret; |
| 3243 | |
| 3244 | /* FIXME: check that next_tss_desc is tss */ |
| 3245 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3246 | /* |
| 3247 | * Check privileges. The three cases are task switch caused by... |
| 3248 | * |
| 3249 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 3250 | * 2. Exception/IRQ/iret: No check is performed |
Nadav Amit | 2c2ca2d | 2014-11-02 11:54:57 +0200 | [diff] [blame] | 3251 | * 3. jmp/call to TSS/task-gate: No check is performed since the |
| 3252 | * hardware checks it before exiting. |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3253 | */ |
| 3254 | if (reason == TASK_SWITCH_GATE) { |
| 3255 | if (idt_index != -1) { |
| 3256 | /* Software interrupts */ |
| 3257 | struct desc_struct task_gate_desc; |
| 3258 | int dpl; |
| 3259 | |
| 3260 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 3261 | &task_gate_desc); |
| 3262 | if (ret != X86EMUL_CONTINUE) |
| 3263 | return ret; |
| 3264 | |
| 3265 | dpl = task_gate_desc.dpl; |
| 3266 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 3267 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 3268 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 3271 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 3272 | if (!next_tss_desc.p || |
| 3273 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 3274 | desc_limit < 0x2b)) { |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3275 | return emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 3279 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3280 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | if (reason == TASK_SWITCH_IRET) |
| 3284 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 3285 | |
| 3286 | /* 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] | 3287 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3288 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 3289 | old_tss_sel = 0xffff; |
| 3290 | |
| 3291 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3292 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3293 | old_tss_base, &next_tss_desc); |
| 3294 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3295 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3296 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 3297 | if (ret != X86EMUL_CONTINUE) |
| 3298 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3299 | |
| 3300 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 3301 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 3302 | |
| 3303 | if (reason != TASK_SWITCH_IRET) { |
| 3304 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3305 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3308 | 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] | 3309 | 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] | 3310 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3311 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3312 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 3313 | ctxt->lock_prefix = 0; |
| 3314 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3315 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3316 | } |
| 3317 | |
Nadav Amit | 3db176d | 2015-04-19 21:12:59 +0300 | [diff] [blame] | 3318 | ops->get_dr(ctxt, 7, &dr7); |
| 3319 | ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN)); |
| 3320 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3321 | return ret; |
| 3322 | } |
| 3323 | |
| 3324 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3325 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3326 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3327 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3328 | int rc; |
| 3329 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3330 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3331 | ctxt->_eip = ctxt->eip; |
| 3332 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3333 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3334 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3335 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3336 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3337 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3338 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3339 | writeback_registers(ctxt); |
| 3340 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3341 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 3342 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 3343 | } |
| 3344 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 3345 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 3346 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3347 | { |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 3348 | int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3349 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 3350 | register_address_increment(ctxt, reg, df * op->bytes); |
| 3351 | op->addr.mem.ea = register_address(ctxt, reg); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3354 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 3355 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3356 | u8 al, old_al; |
| 3357 | bool af, cf, old_cf; |
| 3358 | |
| 3359 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3360 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3361 | |
| 3362 | old_al = al; |
| 3363 | old_cf = cf; |
| 3364 | cf = false; |
| 3365 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 3366 | if ((al & 0x0f) > 9 || af) { |
| 3367 | al -= 6; |
| 3368 | cf = old_cf | (al >= 250); |
| 3369 | af = true; |
| 3370 | } else { |
| 3371 | af = false; |
| 3372 | } |
| 3373 | if (old_al > 0x99 || old_cf) { |
| 3374 | al -= 0x60; |
| 3375 | cf = true; |
| 3376 | } |
| 3377 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3378 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3379 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3380 | ctxt->src.type = OP_IMM; |
| 3381 | ctxt->src.val = 0; |
| 3382 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 3383 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 3384 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 3385 | if (cf) |
| 3386 | ctxt->eflags |= X86_EFLAGS_CF; |
| 3387 | if (af) |
| 3388 | ctxt->eflags |= X86_EFLAGS_AF; |
| 3389 | return X86EMUL_CONTINUE; |
| 3390 | } |
| 3391 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3392 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 3393 | { |
| 3394 | u8 al, ah; |
| 3395 | |
| 3396 | if (ctxt->src.val == 0) |
| 3397 | return emulate_de(ctxt); |
| 3398 | |
| 3399 | al = ctxt->dst.val & 0xff; |
| 3400 | ah = al / ctxt->src.val; |
| 3401 | al %= ctxt->src.val; |
| 3402 | |
| 3403 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 3404 | |
| 3405 | /* Set PF, ZF, SF */ |
| 3406 | ctxt->src.type = OP_IMM; |
| 3407 | ctxt->src.val = 0; |
| 3408 | ctxt->src.bytes = 1; |
| 3409 | fastop(ctxt, em_or); |
| 3410 | |
| 3411 | return X86EMUL_CONTINUE; |
| 3412 | } |
| 3413 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3414 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 3415 | { |
| 3416 | u8 al = ctxt->dst.val & 0xff; |
| 3417 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 3418 | |
| 3419 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 3420 | |
| 3421 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 3422 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 3423 | /* Set PF, ZF, SF */ |
| 3424 | ctxt->src.type = OP_IMM; |
| 3425 | ctxt->src.val = 0; |
| 3426 | ctxt->src.bytes = 1; |
| 3427 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3428 | |
| 3429 | return X86EMUL_CONTINUE; |
| 3430 | } |
| 3431 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3432 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3433 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3434 | int rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3435 | long rel = ctxt->src.val; |
| 3436 | |
| 3437 | ctxt->src.val = (unsigned long)ctxt->_eip; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3438 | rc = jmp_rel(ctxt, rel); |
| 3439 | if (rc != X86EMUL_CONTINUE) |
| 3440 | return rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3441 | return em_push(ctxt); |
| 3442 | } |
| 3443 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3444 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3445 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3446 | u16 sel, old_cs; |
| 3447 | ulong old_eip; |
| 3448 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3449 | struct desc_struct old_desc, new_desc; |
| 3450 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 3451 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3452 | enum x86emul_mode prev_mode = ctxt->mode; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3453 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3454 | old_eip = ctxt->_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3455 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3456 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3457 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3458 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 3459 | X86_TRANSFER_CALL_JMP, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3460 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | 80976db | 2014-12-25 02:52:20 +0200 | [diff] [blame] | 3461 | return rc; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3462 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 3463 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3464 | if (rc != X86EMUL_CONTINUE) |
| 3465 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3466 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3467 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3468 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3469 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3470 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3471 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3472 | ctxt->src.val = old_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3473 | rc = em_push(ctxt); |
| 3474 | /* If we failed, we tainted the memory, but the very least we should |
| 3475 | restore cs */ |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3476 | if (rc != X86EMUL_CONTINUE) { |
| 3477 | pr_warn_once("faulting far call emulation tainted memory\n"); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3478 | goto fail; |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3479 | } |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3480 | return rc; |
| 3481 | fail: |
| 3482 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
Nadav Amit | 8226808 | 2015-01-26 09:32:27 +0200 | [diff] [blame] | 3483 | ctxt->mode = prev_mode; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3484 | return rc; |
| 3485 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3486 | } |
| 3487 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3488 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3489 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3490 | int rc; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3491 | unsigned long eip; |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3492 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3493 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 3494 | if (rc != X86EMUL_CONTINUE) |
| 3495 | return rc; |
| 3496 | rc = assign_eip_near(ctxt, eip); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3497 | if (rc != X86EMUL_CONTINUE) |
| 3498 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3499 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3500 | return X86EMUL_CONTINUE; |
| 3501 | } |
| 3502 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3503 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3504 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3505 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3506 | ctxt->src.val = ctxt->dst.val; |
| 3507 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3508 | |
| 3509 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3510 | ctxt->dst.val = ctxt->src.orig_val; |
| 3511 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3512 | return X86EMUL_CONTINUE; |
| 3513 | } |
| 3514 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3515 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3516 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3517 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3518 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3519 | } |
| 3520 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3521 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3522 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3523 | ctxt->dst.type = OP_REG; |
| 3524 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3525 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3526 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3527 | |
| 3528 | return X86EMUL_CONTINUE; |
| 3529 | } |
| 3530 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3531 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3532 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3533 | u64 tsc = 0; |
| 3534 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3535 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3536 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3537 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3538 | return X86EMUL_CONTINUE; |
| 3539 | } |
| 3540 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3541 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3542 | { |
| 3543 | u64 pmc; |
| 3544 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3545 | 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] | 3546 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3547 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3548 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3549 | return X86EMUL_CONTINUE; |
| 3550 | } |
| 3551 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3552 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3553 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 3554 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3555 | return X86EMUL_CONTINUE; |
| 3556 | } |
| 3557 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3558 | #define FFL(x) bit(X86_FEATURE_##x) |
| 3559 | |
| 3560 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 3561 | { |
| 3562 | u32 ebx, ecx, edx, eax = 1; |
| 3563 | u16 tmp; |
| 3564 | |
| 3565 | /* |
| 3566 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 3567 | */ |
| 3568 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3569 | if (!(ecx & FFL(MOVBE))) |
| 3570 | return emulate_ud(ctxt); |
| 3571 | |
| 3572 | switch (ctxt->op_bytes) { |
| 3573 | case 2: |
| 3574 | /* |
| 3575 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3576 | * the upper word of the destination register remains unchanged |
| 3577 | * ..." |
| 3578 | * |
| 3579 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3580 | * rules so we have to do the operation almost per hand. |
| 3581 | */ |
| 3582 | tmp = (u16)ctxt->src.val; |
| 3583 | ctxt->dst.val &= ~0xffffUL; |
| 3584 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3585 | break; |
| 3586 | case 4: |
| 3587 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3588 | break; |
| 3589 | case 8: |
| 3590 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3591 | break; |
| 3592 | default: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3593 | BUG(); |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3594 | } |
| 3595 | return X86EMUL_CONTINUE; |
| 3596 | } |
| 3597 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3598 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3599 | { |
| 3600 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3601 | return emulate_gp(ctxt, 0); |
| 3602 | |
| 3603 | /* Disable writeback. */ |
| 3604 | ctxt->dst.type = OP_NONE; |
| 3605 | return X86EMUL_CONTINUE; |
| 3606 | } |
| 3607 | |
| 3608 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3609 | { |
| 3610 | unsigned long val; |
| 3611 | |
| 3612 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3613 | val = ctxt->src.val & ~0ULL; |
| 3614 | else |
| 3615 | val = ctxt->src.val & ~0U; |
| 3616 | |
| 3617 | /* #UD condition is already handled. */ |
| 3618 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3619 | return emulate_gp(ctxt, 0); |
| 3620 | |
| 3621 | /* Disable writeback. */ |
| 3622 | ctxt->dst.type = OP_NONE; |
| 3623 | return X86EMUL_CONTINUE; |
| 3624 | } |
| 3625 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3626 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3627 | { |
| 3628 | u64 msr_data; |
| 3629 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3630 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3631 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3632 | 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] | 3633 | return emulate_gp(ctxt, 0); |
| 3634 | |
| 3635 | return X86EMUL_CONTINUE; |
| 3636 | } |
| 3637 | |
| 3638 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3639 | { |
| 3640 | u64 msr_data; |
| 3641 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3642 | 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] | 3643 | return emulate_gp(ctxt, 0); |
| 3644 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3645 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3646 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3647 | return X86EMUL_CONTINUE; |
| 3648 | } |
| 3649 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3650 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3651 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3652 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3653 | return emulate_ud(ctxt); |
| 3654 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3655 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Nadav Amit | b5bbf10 | 2014-11-02 11:54:46 +0200 | [diff] [blame] | 3656 | if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM) |
| 3657 | ctxt->dst.bytes = 2; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3658 | return X86EMUL_CONTINUE; |
| 3659 | } |
| 3660 | |
| 3661 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3662 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3663 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3664 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3665 | 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] | 3666 | return emulate_ud(ctxt); |
| 3667 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3668 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3669 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3670 | |
| 3671 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3672 | ctxt->dst.type = OP_NONE; |
| 3673 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3674 | } |
| 3675 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3676 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3677 | { |
| 3678 | u16 sel = ctxt->src.val; |
| 3679 | |
| 3680 | /* Disable writeback. */ |
| 3681 | ctxt->dst.type = OP_NONE; |
| 3682 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3683 | } |
| 3684 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3685 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3686 | { |
| 3687 | u16 sel = ctxt->src.val; |
| 3688 | |
| 3689 | /* Disable writeback. */ |
| 3690 | ctxt->dst.type = OP_NONE; |
| 3691 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3692 | } |
| 3693 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3694 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3695 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3696 | int rc; |
| 3697 | ulong linear; |
| 3698 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3699 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3700 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3701 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3702 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3703 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3704 | return X86EMUL_CONTINUE; |
| 3705 | } |
| 3706 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3707 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3708 | { |
| 3709 | ulong cr0; |
| 3710 | |
| 3711 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3712 | cr0 &= ~X86_CR0_TS; |
| 3713 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3714 | return X86EMUL_CONTINUE; |
| 3715 | } |
| 3716 | |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 3717 | static int em_hypercall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3718 | { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3719 | int rc = ctxt->ops->fix_hypercall(ctxt); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3720 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3721 | if (rc != X86EMUL_CONTINUE) |
| 3722 | return rc; |
| 3723 | |
| 3724 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3725 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3726 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3727 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3728 | return X86EMUL_CONTINUE; |
| 3729 | } |
| 3730 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3731 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3732 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3733 | struct desc_ptr *ptr)) |
| 3734 | { |
| 3735 | struct desc_ptr desc_ptr; |
| 3736 | |
| 3737 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3738 | ctxt->op_bytes = 8; |
| 3739 | get(ctxt, &desc_ptr); |
| 3740 | if (ctxt->op_bytes == 2) { |
| 3741 | ctxt->op_bytes = 4; |
| 3742 | desc_ptr.address &= 0x00ffffff; |
| 3743 | } |
| 3744 | /* Disable writeback. */ |
| 3745 | ctxt->dst.type = OP_NONE; |
Steve Rutherford | 736e77c0 | 2017-01-11 18:28:29 -0800 | [diff] [blame] | 3746 | return segmented_write_std(ctxt, ctxt->dst.addr.mem, |
| 3747 | &desc_ptr, 2 + ctxt->op_bytes); |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3751 | { |
| 3752 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3753 | } |
| 3754 | |
| 3755 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3756 | { |
| 3757 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3758 | } |
| 3759 | |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3760 | static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3761 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3762 | struct desc_ptr desc_ptr; |
| 3763 | int rc; |
| 3764 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3765 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3766 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3767 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3768 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3769 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3770 | if (rc != X86EMUL_CONTINUE) |
| 3771 | return rc; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 3772 | if (ctxt->mode == X86EMUL_MODE_PROT64 && |
| 3773 | is_noncanonical_address(desc_ptr.address)) |
| 3774 | return emulate_gp(ctxt, 0); |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3775 | if (lgdt) |
| 3776 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3777 | else |
| 3778 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3779 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3780 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3781 | return X86EMUL_CONTINUE; |
| 3782 | } |
| 3783 | |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3784 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3785 | { |
| 3786 | return em_lgdt_lidt(ctxt, true); |
| 3787 | } |
| 3788 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3789 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3790 | { |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3791 | return em_lgdt_lidt(ctxt, false); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3795 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3796 | if (ctxt->dst.type == OP_MEM) |
| 3797 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3798 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3799 | return X86EMUL_CONTINUE; |
| 3800 | } |
| 3801 | |
| 3802 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3803 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3804 | 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] | 3805 | | (ctxt->src.val & 0x0f)); |
| 3806 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3807 | return X86EMUL_CONTINUE; |
| 3808 | } |
| 3809 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3810 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3811 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3812 | int rc = X86EMUL_CONTINUE; |
| 3813 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 3814 | register_address_increment(ctxt, VCPU_REGS_RCX, -1); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3815 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3816 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3817 | rc = jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3818 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3819 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3823 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3824 | int rc = X86EMUL_CONTINUE; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3825 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3826 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
| 3827 | rc = jmp_rel(ctxt, ctxt->src.val); |
| 3828 | |
| 3829 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3830 | } |
| 3831 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3832 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3833 | { |
| 3834 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3835 | &ctxt->dst.val)) |
| 3836 | return X86EMUL_IO_NEEDED; |
| 3837 | |
| 3838 | return X86EMUL_CONTINUE; |
| 3839 | } |
| 3840 | |
| 3841 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3842 | { |
| 3843 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3844 | &ctxt->src.val, 1); |
| 3845 | /* Disable writeback. */ |
| 3846 | ctxt->dst.type = OP_NONE; |
| 3847 | return X86EMUL_CONTINUE; |
| 3848 | } |
| 3849 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3850 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3851 | { |
| 3852 | if (emulator_bad_iopl(ctxt)) |
| 3853 | return emulate_gp(ctxt, 0); |
| 3854 | |
| 3855 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3856 | return X86EMUL_CONTINUE; |
| 3857 | } |
| 3858 | |
| 3859 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3860 | { |
| 3861 | if (emulator_bad_iopl(ctxt)) |
| 3862 | return emulate_gp(ctxt, 0); |
| 3863 | |
| 3864 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3865 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3866 | return X86EMUL_CONTINUE; |
| 3867 | } |
| 3868 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3869 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3870 | { |
| 3871 | u32 eax, ebx, ecx, edx; |
| 3872 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3873 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3874 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3875 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3876 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3877 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3878 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3879 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3880 | return X86EMUL_CONTINUE; |
| 3881 | } |
| 3882 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3883 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3884 | { |
| 3885 | u32 flags; |
| 3886 | |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 3887 | flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 3888 | X86_EFLAGS_SF; |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3889 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3890 | |
| 3891 | ctxt->eflags &= ~0xffUL; |
| 3892 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3893 | return X86EMUL_CONTINUE; |
| 3894 | } |
| 3895 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3896 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3897 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3898 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3899 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3900 | return X86EMUL_CONTINUE; |
| 3901 | } |
| 3902 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3903 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3904 | { |
| 3905 | switch (ctxt->op_bytes) { |
| 3906 | #ifdef CONFIG_X86_64 |
| 3907 | case 8: |
| 3908 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3909 | break; |
| 3910 | #endif |
| 3911 | default: |
| 3912 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3913 | break; |
| 3914 | } |
| 3915 | return X86EMUL_CONTINUE; |
| 3916 | } |
| 3917 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3918 | static int em_clflush(struct x86_emulate_ctxt *ctxt) |
| 3919 | { |
| 3920 | /* emulating clflush regardless of cpuid */ |
| 3921 | return X86EMUL_CONTINUE; |
| 3922 | } |
| 3923 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 3924 | static int em_movsxd(struct x86_emulate_ctxt *ctxt) |
| 3925 | { |
| 3926 | ctxt->dst.val = (s32) ctxt->src.val; |
| 3927 | return X86EMUL_CONTINUE; |
| 3928 | } |
| 3929 | |
Radim Krčmář | 83fedbb | 2016-11-09 19:07:06 +0100 | [diff] [blame] | 3930 | static int check_fxsr(struct x86_emulate_ctxt *ctxt) |
| 3931 | { |
| 3932 | u32 eax = 1, ebx, ecx = 0, edx; |
| 3933 | |
| 3934 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3935 | if (!(edx & FFL(FXSR))) |
| 3936 | return emulate_ud(ctxt); |
| 3937 | |
| 3938 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 3939 | return emulate_nm(ctxt); |
| 3940 | |
| 3941 | /* |
| 3942 | * Don't emulate a case that should never be hit, instead of working |
| 3943 | * around a lack of fxsave64/fxrstor64 on old compilers. |
| 3944 | */ |
| 3945 | if (ctxt->mode >= X86EMUL_MODE_PROT64) |
| 3946 | return X86EMUL_UNHANDLEABLE; |
| 3947 | |
| 3948 | return X86EMUL_CONTINUE; |
| 3949 | } |
| 3950 | |
| 3951 | /* |
| 3952 | * FXSAVE and FXRSTOR have 4 different formats depending on execution mode, |
| 3953 | * 1) 16 bit mode |
| 3954 | * 2) 32 bit mode |
| 3955 | * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs |
| 3956 | * preserve whole 32 bit values, though, so (1) and (2) are the same wrt. |
| 3957 | * save and restore |
| 3958 | * 3) 64-bit mode with REX.W prefix |
| 3959 | * - like (2), but XMM 8-15 are being saved and restored |
| 3960 | * 4) 64-bit mode without REX.W prefix |
| 3961 | * - like (3), but FIP and FDP are 64 bit |
| 3962 | * |
| 3963 | * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the |
| 3964 | * desired result. (4) is not emulated. |
| 3965 | * |
| 3966 | * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS |
| 3967 | * and FPU DS) should match. |
| 3968 | */ |
| 3969 | static int em_fxsave(struct x86_emulate_ctxt *ctxt) |
| 3970 | { |
| 3971 | struct fxregs_state fx_state; |
| 3972 | size_t size; |
| 3973 | int rc; |
| 3974 | |
| 3975 | rc = check_fxsr(ctxt); |
| 3976 | if (rc != X86EMUL_CONTINUE) |
| 3977 | return rc; |
| 3978 | |
| 3979 | ctxt->ops->get_fpu(ctxt); |
| 3980 | |
| 3981 | rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state)); |
| 3982 | |
| 3983 | ctxt->ops->put_fpu(ctxt); |
| 3984 | |
| 3985 | if (rc != X86EMUL_CONTINUE) |
| 3986 | return rc; |
| 3987 | |
| 3988 | if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR) |
| 3989 | size = offsetof(struct fxregs_state, xmm_space[8 * 16/4]); |
| 3990 | else |
| 3991 | size = offsetof(struct fxregs_state, xmm_space[0]); |
| 3992 | |
Steve Rutherford | 736e77c0 | 2017-01-11 18:28:29 -0800 | [diff] [blame] | 3993 | return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state, size); |
Radim Krčmář | 83fedbb | 2016-11-09 19:07:06 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | static int fxrstor_fixup(struct x86_emulate_ctxt *ctxt, |
| 3997 | struct fxregs_state *new) |
| 3998 | { |
| 3999 | int rc = X86EMUL_CONTINUE; |
| 4000 | struct fxregs_state old; |
| 4001 | |
| 4002 | rc = asm_safe("fxsave %[fx]", , [fx] "+m"(old)); |
| 4003 | if (rc != X86EMUL_CONTINUE) |
| 4004 | return rc; |
| 4005 | |
| 4006 | /* |
| 4007 | * 64 bit host will restore XMM 8-15, which is not correct on non-64 |
| 4008 | * bit guests. Load the current values in order to preserve 64 bit |
| 4009 | * XMMs after fxrstor. |
| 4010 | */ |
| 4011 | #ifdef CONFIG_X86_64 |
| 4012 | /* XXX: accessing XMM 8-15 very awkwardly */ |
| 4013 | memcpy(&new->xmm_space[8 * 16/4], &old.xmm_space[8 * 16/4], 8 * 16); |
| 4014 | #endif |
| 4015 | |
| 4016 | /* |
| 4017 | * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but |
| 4018 | * does save and restore MXCSR. |
| 4019 | */ |
| 4020 | if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR)) |
| 4021 | memcpy(new->xmm_space, old.xmm_space, 8 * 16); |
| 4022 | |
| 4023 | return rc; |
| 4024 | } |
| 4025 | |
| 4026 | static int em_fxrstor(struct x86_emulate_ctxt *ctxt) |
| 4027 | { |
| 4028 | struct fxregs_state fx_state; |
| 4029 | int rc; |
| 4030 | |
| 4031 | rc = check_fxsr(ctxt); |
| 4032 | if (rc != X86EMUL_CONTINUE) |
| 4033 | return rc; |
| 4034 | |
Steve Rutherford | 736e77c0 | 2017-01-11 18:28:29 -0800 | [diff] [blame] | 4035 | rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, 512); |
Radim Krčmář | 83fedbb | 2016-11-09 19:07:06 +0100 | [diff] [blame] | 4036 | if (rc != X86EMUL_CONTINUE) |
| 4037 | return rc; |
| 4038 | |
| 4039 | if (fx_state.mxcsr >> 16) |
| 4040 | return emulate_gp(ctxt, 0); |
| 4041 | |
| 4042 | ctxt->ops->get_fpu(ctxt); |
| 4043 | |
| 4044 | if (ctxt->mode < X86EMUL_MODE_PROT64) |
| 4045 | rc = fxrstor_fixup(ctxt, &fx_state); |
| 4046 | |
| 4047 | if (rc == X86EMUL_CONTINUE) |
| 4048 | rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state)); |
| 4049 | |
| 4050 | ctxt->ops->put_fpu(ctxt); |
| 4051 | |
| 4052 | return rc; |
| 4053 | } |
| 4054 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4055 | static bool valid_cr(int nr) |
| 4056 | { |
| 4057 | switch (nr) { |
| 4058 | case 0: |
| 4059 | case 2 ... 4: |
| 4060 | case 8: |
| 4061 | return true; |
| 4062 | default: |
| 4063 | return false; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 4068 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4069 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4070 | return emulate_ud(ctxt); |
| 4071 | |
| 4072 | return X86EMUL_CONTINUE; |
| 4073 | } |
| 4074 | |
| 4075 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 4076 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4077 | u64 new_val = ctxt->src.val64; |
| 4078 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 4079 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4080 | |
| 4081 | static u64 cr_reserved_bits[] = { |
| 4082 | 0xffffffff00000000ULL, |
| 4083 | 0, 0, 0, /* CR3 checked later */ |
| 4084 | CR4_RESERVED_BITS, |
| 4085 | 0, 0, 0, |
| 4086 | CR8_RESERVED_BITS, |
| 4087 | }; |
| 4088 | |
| 4089 | if (!valid_cr(cr)) |
| 4090 | return emulate_ud(ctxt); |
| 4091 | |
| 4092 | if (new_val & cr_reserved_bits[cr]) |
| 4093 | return emulate_gp(ctxt, 0); |
| 4094 | |
| 4095 | switch (cr) { |
| 4096 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 4097 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4098 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 4099 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 4100 | return emulate_gp(ctxt, 0); |
| 4101 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4102 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 4103 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4104 | |
| 4105 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 4106 | !(cr4 & X86_CR4_PAE)) |
| 4107 | return emulate_gp(ctxt, 0); |
| 4108 | |
| 4109 | break; |
| 4110 | } |
| 4111 | case 3: { |
| 4112 | u64 rsvd = 0; |
| 4113 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 4114 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 4115 | if (efer & EFER_LMA) |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 4116 | rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4117 | |
| 4118 | if (new_val & rsvd) |
| 4119 | return emulate_gp(ctxt, 0); |
| 4120 | |
| 4121 | break; |
| 4122 | } |
| 4123 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4124 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4125 | |
| 4126 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 4127 | return emulate_gp(ctxt, 0); |
| 4128 | |
| 4129 | break; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | return X86EMUL_CONTINUE; |
| 4134 | } |
| 4135 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4136 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 4137 | { |
| 4138 | unsigned long dr7; |
| 4139 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4140 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4141 | |
| 4142 | /* Check if DR7.Global_Enable is set */ |
| 4143 | return dr7 & (1 << 13); |
| 4144 | } |
| 4145 | |
| 4146 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 4147 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4148 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4149 | u64 cr4; |
| 4150 | |
| 4151 | if (dr > 7) |
| 4152 | return emulate_ud(ctxt); |
| 4153 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4154 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4155 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 4156 | return emulate_ud(ctxt); |
| 4157 | |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 4158 | if (check_dr7_gd(ctxt)) { |
| 4159 | ulong dr6; |
| 4160 | |
| 4161 | ctxt->ops->get_dr(ctxt, 6, &dr6); |
| 4162 | dr6 &= ~15; |
| 4163 | dr6 |= DR6_BD | DR6_RTM; |
| 4164 | ctxt->ops->set_dr(ctxt, 6, dr6); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4165 | return emulate_db(ctxt); |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 4166 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4167 | |
| 4168 | return X86EMUL_CONTINUE; |
| 4169 | } |
| 4170 | |
| 4171 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 4172 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4173 | u64 new_val = ctxt->src.val64; |
| 4174 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4175 | |
| 4176 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 4177 | return emulate_gp(ctxt, 0); |
| 4178 | |
| 4179 | return check_dr_read(ctxt); |
| 4180 | } |
| 4181 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4182 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 4183 | { |
| 4184 | u64 efer; |
| 4185 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4186 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4187 | |
| 4188 | if (!(efer & EFER_SVME)) |
| 4189 | return emulate_ud(ctxt); |
| 4190 | |
| 4191 | return X86EMUL_CONTINUE; |
| 4192 | } |
| 4193 | |
| 4194 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 4195 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4196 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4197 | |
| 4198 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 4199 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4200 | return emulate_gp(ctxt, 0); |
| 4201 | |
| 4202 | return check_svme(ctxt); |
| 4203 | } |
| 4204 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4205 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 4206 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4207 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4208 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4209 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4210 | return emulate_ud(ctxt); |
| 4211 | |
| 4212 | return X86EMUL_CONTINUE; |
| 4213 | } |
| 4214 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4215 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 4216 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4217 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4218 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4219 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 4220 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 4221 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4222 | return emulate_gp(ctxt, 0); |
| 4223 | |
| 4224 | return X86EMUL_CONTINUE; |
| 4225 | } |
| 4226 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4227 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 4228 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4229 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 4230 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4231 | return emulate_gp(ctxt, 0); |
| 4232 | |
| 4233 | return X86EMUL_CONTINUE; |
| 4234 | } |
| 4235 | |
| 4236 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 4237 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4238 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 4239 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4240 | return emulate_gp(ctxt, 0); |
| 4241 | |
| 4242 | return X86EMUL_CONTINUE; |
| 4243 | } |
| 4244 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4245 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4246 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 4247 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 4248 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 4249 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4250 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4251 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 4252 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4253 | #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) } |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4254 | #define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4255 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4256 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4257 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4258 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4259 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4260 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4261 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 4262 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4263 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4264 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4265 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4266 | #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] | 4267 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 4268 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4269 | #define I2bvIP(_f, _e, _i, _p) \ |
| 4270 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4271 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4272 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 4273 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 4274 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 4275 | |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 4276 | static const struct opcode group7_rm0[] = { |
| 4277 | N, |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 4278 | I(SrcNone | Priv | EmulateOnUD, em_hypercall), |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 4279 | N, N, N, N, N, N, |
| 4280 | }; |
| 4281 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4282 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4283 | DI(SrcNone | Priv, monitor), |
| 4284 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4285 | N, N, N, N, N, N, |
| 4286 | }; |
| 4287 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4288 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4289 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Jan Kiszka | b34a805 | 2015-03-09 20:27:43 +0100 | [diff] [blame] | 4290 | II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4291 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 4292 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 4293 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 4294 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 4295 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 4296 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4297 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 4298 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4299 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4300 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4301 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4302 | N, N, N, N, N, N, |
| 4303 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4304 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4305 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4306 | F(Lock, em_add), |
| 4307 | F(Lock | PageTable, em_or), |
| 4308 | F(Lock, em_adc), |
| 4309 | F(Lock, em_sbb), |
| 4310 | F(Lock | PageTable, em_and), |
| 4311 | F(Lock, em_sub), |
| 4312 | F(Lock, em_xor), |
| 4313 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4314 | }; |
| 4315 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4316 | static const struct opcode group1A[] = { |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 4317 | 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] | 4318 | }; |
| 4319 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4320 | static const struct opcode group2[] = { |
| 4321 | F(DstMem | ModRM, em_rol), |
| 4322 | F(DstMem | ModRM, em_ror), |
| 4323 | F(DstMem | ModRM, em_rcl), |
| 4324 | F(DstMem | ModRM, em_rcr), |
| 4325 | F(DstMem | ModRM, em_shl), |
| 4326 | F(DstMem | ModRM, em_shr), |
| 4327 | F(DstMem | ModRM, em_shl), |
| 4328 | F(DstMem | ModRM, em_sar), |
| 4329 | }; |
| 4330 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4331 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4332 | F(DstMem | SrcImm | NoWrite, em_test), |
| 4333 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 4334 | F(DstMem | SrcNone | Lock, em_not), |
| 4335 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4336 | F(DstXacc | Src2Mem, em_mul_ex), |
| 4337 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4338 | F(DstXacc | Src2Mem, em_div_ex), |
| 4339 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4340 | }; |
| 4341 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4342 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 4343 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 4344 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4345 | N, N, N, N, N, N, |
| 4346 | }; |
| 4347 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4348 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 4349 | F(DstMem | SrcNone | Lock, em_inc), |
| 4350 | F(DstMem | SrcNone | Lock, em_dec), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4351 | I(SrcMem | NearBranch, em_call_near_abs), |
Nadav Amit | acac6f89 | 2015-05-03 20:22:57 +0300 | [diff] [blame] | 4352 | I(SrcMemFAddr | ImplicitOps, em_call_far), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4353 | I(SrcMem | NearBranch, em_jmp_abs), |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 4354 | I(SrcMemFAddr | ImplicitOps, em_jmp_far), |
| 4355 | I(SrcMem | Stack, em_push), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4356 | }; |
| 4357 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4358 | static const struct opcode group6[] = { |
Nadav Amit | 63ea0a4 | 2015-01-08 11:59:03 +0100 | [diff] [blame] | 4359 | DI(Prot | DstMem, sldt), |
| 4360 | DI(Prot | DstMem, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 4361 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 4362 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4363 | N, N, N, N, |
| 4364 | }; |
| 4365 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4366 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 4367 | II(Mov | DstMem, em_sgdt, sgdt), |
| 4368 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4369 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 4370 | II(SrcMem | Priv, em_lidt, lidt), |
| 4371 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 4372 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 4373 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4374 | }, { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 4375 | EXT(0, group7_rm0), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 4376 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4377 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4378 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 4379 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 4380 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4381 | } }; |
| 4382 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4383 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4384 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4385 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 4386 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 4387 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 4388 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4389 | }; |
| 4390 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4391 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4392 | 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] | 4393 | }, { |
| 4394 | N, N, N, N, N, N, N, N, |
| 4395 | } }; |
| 4396 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4397 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4398 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4399 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4400 | }; |
| 4401 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4402 | static const struct gprefix pfx_0f_ae_7 = { |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4403 | I(SrcMem | ByteOp, em_clflush), N, N, N, |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4404 | }; |
| 4405 | |
| 4406 | static const struct group_dual group15 = { { |
Radim Krčmář | 83fedbb | 2016-11-09 19:07:06 +0100 | [diff] [blame] | 4407 | I(ModRM | Aligned16, em_fxsave), |
| 4408 | I(ModRM | Aligned16, em_fxrstor), |
| 4409 | N, N, N, N, N, GP(0, &pfx_0f_ae_7), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4410 | }, { |
| 4411 | N, N, N, N, N, N, N, N, |
| 4412 | } }; |
| 4413 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4414 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 4415 | 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] | 4416 | }; |
| 4417 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4418 | static const struct instr_dual instr_dual_0f_2b = { |
| 4419 | I(0, em_mov), N |
| 4420 | }; |
| 4421 | |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4422 | static const struct gprefix pfx_0f_2b = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4423 | 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] | 4424 | }; |
| 4425 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4426 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 4427 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4428 | }; |
| 4429 | |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4430 | static const struct gprefix pfx_0f_e7 = { |
| 4431 | N, I(Sse, em_mov), N, N, |
| 4432 | }; |
| 4433 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4434 | static const struct escape escape_d9 = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 4435 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4436 | }, { |
| 4437 | /* 0xC0 - 0xC7 */ |
| 4438 | N, N, N, N, N, N, N, N, |
| 4439 | /* 0xC8 - 0xCF */ |
| 4440 | N, N, N, N, N, N, N, N, |
| 4441 | /* 0xD0 - 0xC7 */ |
| 4442 | N, N, N, N, N, N, N, N, |
| 4443 | /* 0xD8 - 0xDF */ |
| 4444 | N, N, N, N, N, N, N, N, |
| 4445 | /* 0xE0 - 0xE7 */ |
| 4446 | N, N, N, N, N, N, N, N, |
| 4447 | /* 0xE8 - 0xEF */ |
| 4448 | N, N, N, N, N, N, N, N, |
| 4449 | /* 0xF0 - 0xF7 */ |
| 4450 | N, N, N, N, N, N, N, N, |
| 4451 | /* 0xF8 - 0xFF */ |
| 4452 | N, N, N, N, N, N, N, N, |
| 4453 | } }; |
| 4454 | |
| 4455 | static const struct escape escape_db = { { |
| 4456 | N, N, N, N, N, N, N, N, |
| 4457 | }, { |
| 4458 | /* 0xC0 - 0xC7 */ |
| 4459 | N, N, N, N, N, N, N, N, |
| 4460 | /* 0xC8 - 0xCF */ |
| 4461 | N, N, N, N, N, N, N, N, |
| 4462 | /* 0xD0 - 0xC7 */ |
| 4463 | N, N, N, N, N, N, N, N, |
| 4464 | /* 0xD8 - 0xDF */ |
| 4465 | N, N, N, N, N, N, N, N, |
| 4466 | /* 0xE0 - 0xE7 */ |
| 4467 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 4468 | /* 0xE8 - 0xEF */ |
| 4469 | N, N, N, N, N, N, N, N, |
| 4470 | /* 0xF0 - 0xF7 */ |
| 4471 | N, N, N, N, N, N, N, N, |
| 4472 | /* 0xF8 - 0xFF */ |
| 4473 | N, N, N, N, N, N, N, N, |
| 4474 | } }; |
| 4475 | |
| 4476 | static const struct escape escape_dd = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 4477 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4478 | }, { |
| 4479 | /* 0xC0 - 0xC7 */ |
| 4480 | N, N, N, N, N, N, N, N, |
| 4481 | /* 0xC8 - 0xCF */ |
| 4482 | N, N, N, N, N, N, N, N, |
| 4483 | /* 0xD0 - 0xC7 */ |
| 4484 | N, N, N, N, N, N, N, N, |
| 4485 | /* 0xD8 - 0xDF */ |
| 4486 | N, N, N, N, N, N, N, N, |
| 4487 | /* 0xE0 - 0xE7 */ |
| 4488 | N, N, N, N, N, N, N, N, |
| 4489 | /* 0xE8 - 0xEF */ |
| 4490 | N, N, N, N, N, N, N, N, |
| 4491 | /* 0xF0 - 0xF7 */ |
| 4492 | N, N, N, N, N, N, N, N, |
| 4493 | /* 0xF8 - 0xFF */ |
| 4494 | N, N, N, N, N, N, N, N, |
| 4495 | } }; |
| 4496 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4497 | static const struct instr_dual instr_dual_0f_c3 = { |
| 4498 | I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N |
| 4499 | }; |
| 4500 | |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4501 | static const struct mode_dual mode_dual_63 = { |
| 4502 | N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd) |
| 4503 | }; |
| 4504 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4505 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4506 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4507 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4508 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 4509 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4510 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4511 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4512 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 4513 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4514 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4515 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4516 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 4517 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4518 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4519 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4520 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 4521 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4522 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4523 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4524 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4525 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4526 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4527 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4528 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4529 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4530 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 4531 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4532 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 4533 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4534 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 4535 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4536 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 4537 | I(ImplicitOps | Stack | No64, em_pusha), |
| 4538 | I(ImplicitOps | Stack | No64, em_popa), |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4539 | N, MD(ModRM, &mode_dual_63), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4540 | N, N, N, N, |
| 4541 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 4542 | I(SrcImm | Mov | Stack, em_push), |
| 4543 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 4544 | I(SrcImmByte | Mov | Stack, em_push), |
| 4545 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4546 | 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] | 4547 | 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] | 4548 | /* 0x70 - 0x7F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4549 | X16(D(SrcImmByte | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4550 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4551 | G(ByteOp | DstMem | SrcImm, group1), |
| 4552 | G(DstMem | SrcImm, group1), |
| 4553 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 4554 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4555 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4556 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4557 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4558 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4559 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4560 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 4561 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 4562 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 4563 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4564 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4565 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4566 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 4567 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 4568 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 4569 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 4570 | II(ImplicitOps | Stack, em_popf, popf), |
| 4571 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4572 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4573 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4574 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4575 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4576 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4577 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4578 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4579 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 4580 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4581 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4582 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4583 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4584 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4585 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4586 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4587 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4588 | I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm), |
| 4589 | I(ImplicitOps | NearBranch, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4590 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 4591 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4592 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4593 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 4594 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Nadav Amit | 16794aa | 2015-01-26 09:32:22 +0200 | [diff] [blame] | 4595 | I(ImplicitOps | SrcImmU16, em_ret_far_imm), |
| 4596 | I(ImplicitOps, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4597 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4598 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4599 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4600 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 4601 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 4602 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 4603 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 4604 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4605 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4606 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4607 | 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] | 4608 | /* 0xE0 - 0xE7 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4609 | X3(I(SrcImmByte | NearBranch, em_loop)), |
| 4610 | I(SrcImmByte | NearBranch, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4611 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 4612 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4613 | /* 0xE8 - 0xEF */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4614 | I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch), |
| 4615 | I(SrcImmFAddr | No64, em_jmp_far), |
| 4616 | D(SrcImmByte | ImplicitOps | NearBranch), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4617 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 4618 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4619 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4620 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4621 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 4622 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4623 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 4624 | D(ImplicitOps), D(ImplicitOps), |
| 4625 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4626 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 4627 | }; |
| 4628 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4629 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4630 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4631 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4632 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4633 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4634 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4635 | N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4636 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4637 | N, N, N, N, N, N, N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4638 | D(ImplicitOps | ModRM | SrcMem | NoAccess), |
| 4639 | N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4640 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 4641 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 4642 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 4643 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 4644 | check_cr_write), |
| 4645 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 4646 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4647 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4648 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 4649 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4650 | N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4651 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4652 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4653 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4654 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4655 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4656 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4657 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 4658 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4659 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4660 | N, N, N, N, N, N, N, N, |
| 4661 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4662 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4663 | /* 0x50 - 0x5F */ |
| 4664 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4665 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4666 | N, N, N, N, |
| 4667 | N, N, N, N, |
| 4668 | N, N, N, N, |
| 4669 | 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] | 4670 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4671 | N, N, N, N, |
| 4672 | N, N, N, N, |
| 4673 | N, N, N, N, |
| 4674 | 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] | 4675 | /* 0x80 - 0x8F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4676 | X16(D(SrcImm | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4677 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4678 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4679 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4680 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4681 | II(ImplicitOps, em_cpuid, cpuid), |
| 4682 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4683 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4684 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4685 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4686 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Paolo Bonzini | 89651a3 | 2015-11-03 13:43:05 +0100 | [diff] [blame] | 4687 | II(EmulateOnUD | ImplicitOps, em_rsm, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4688 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4689 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4690 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4691 | GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4692 | /* 0xB0 - 0xB7 */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 4693 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4694 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4695 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4696 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4697 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4698 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4699 | /* 0xB8 - 0xBF */ |
| 4700 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4701 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4702 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
Nadav Amit | 900efe2 | 2015-03-30 15:39:21 +0300 | [diff] [blame] | 4703 | I(DstReg | SrcMem | ModRM, em_bsf_c), |
| 4704 | I(DstReg | SrcMem | ModRM, em_bsr_c), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4705 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4706 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 4707 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4708 | N, ID(0, &instr_dual_0f_c3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4709 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4710 | /* 0xC8 - 0xCF */ |
| 4711 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4712 | /* 0xD0 - 0xDF */ |
| 4713 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4714 | /* 0xE0 - 0xEF */ |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4715 | N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), |
| 4716 | N, N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4717 | /* 0xF0 - 0xFF */ |
| 4718 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4719 | }; |
| 4720 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4721 | static const struct instr_dual instr_dual_0f_38_f0 = { |
| 4722 | I(DstReg | SrcMem | Mov, em_movbe), N |
| 4723 | }; |
| 4724 | |
| 4725 | static const struct instr_dual instr_dual_0f_38_f1 = { |
| 4726 | I(DstMem | SrcReg | Mov, em_movbe), N |
| 4727 | }; |
| 4728 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4729 | static const struct gprefix three_byte_0f_38_f0 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4730 | ID(0, &instr_dual_0f_38_f0), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4731 | }; |
| 4732 | |
| 4733 | static const struct gprefix three_byte_0f_38_f1 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4734 | ID(0, &instr_dual_0f_38_f1), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4735 | }; |
| 4736 | |
| 4737 | /* |
| 4738 | * Insns below are selected by the prefix which indexed by the third opcode |
| 4739 | * byte. |
| 4740 | */ |
| 4741 | static const struct opcode opcode_map_0f_38[256] = { |
| 4742 | /* 0x00 - 0x7f */ |
| 4743 | 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] | 4744 | /* 0x80 - 0xef */ |
| 4745 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 4746 | /* 0xf0 - 0xf1 */ |
Nadav Amit | 53bb4f7 | 2014-12-07 11:49:42 +0200 | [diff] [blame] | 4747 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), |
| 4748 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1), |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4749 | /* 0xf2 - 0xff */ |
| 4750 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4751 | }; |
| 4752 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4753 | #undef D |
| 4754 | #undef N |
| 4755 | #undef G |
| 4756 | #undef GD |
| 4757 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4758 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4759 | #undef EXT |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 4760 | #undef MD |
Nadav Amit | 2b42fce | 2015-01-26 09:32:25 +0200 | [diff] [blame] | 4761 | #undef ID |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4762 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4763 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4764 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4765 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4766 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4767 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4768 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4769 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4770 | { |
| 4771 | unsigned size; |
| 4772 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4773 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4774 | if (size == 8) |
| 4775 | size = 4; |
| 4776 | return size; |
| 4777 | } |
| 4778 | |
| 4779 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4780 | unsigned size, bool sign_extension) |
| 4781 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4782 | int rc = X86EMUL_CONTINUE; |
| 4783 | |
| 4784 | op->type = OP_IMM; |
| 4785 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4786 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4787 | /* NB. Immediates are sign-extended as necessary. */ |
| 4788 | switch (op->bytes) { |
| 4789 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4790 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4791 | break; |
| 4792 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4793 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4794 | break; |
| 4795 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4796 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4797 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4798 | case 8: |
| 4799 | op->val = insn_fetch(s64, ctxt); |
| 4800 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4801 | } |
| 4802 | if (!sign_extension) { |
| 4803 | switch (op->bytes) { |
| 4804 | case 1: |
| 4805 | op->val &= 0xff; |
| 4806 | break; |
| 4807 | case 2: |
| 4808 | op->val &= 0xffff; |
| 4809 | break; |
| 4810 | case 4: |
| 4811 | op->val &= 0xffffffff; |
| 4812 | break; |
| 4813 | } |
| 4814 | } |
| 4815 | done: |
| 4816 | return rc; |
| 4817 | } |
| 4818 | |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4819 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4820 | unsigned d) |
| 4821 | { |
| 4822 | int rc = X86EMUL_CONTINUE; |
| 4823 | |
| 4824 | switch (d) { |
| 4825 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4826 | decode_register_operand(ctxt, op); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4827 | break; |
| 4828 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4829 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4830 | break; |
| 4831 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4832 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4833 | mem_common: |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4834 | *op = ctxt->memop; |
| 4835 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4836 | if (ctxt->d & BitOp) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4837 | fetch_bit_operand(ctxt); |
| 4838 | op->orig_val = op->val; |
| 4839 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4840 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4841 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4842 | goto mem_common; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4843 | case OpAcc: |
| 4844 | op->type = OP_REG; |
| 4845 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4846 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4847 | fetch_register_operand(op); |
| 4848 | op->orig_val = op->val; |
| 4849 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4850 | case OpAccLo: |
| 4851 | op->type = OP_REG; |
| 4852 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4853 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4854 | fetch_register_operand(op); |
| 4855 | op->orig_val = op->val; |
| 4856 | break; |
| 4857 | case OpAccHi: |
| 4858 | if (ctxt->d & ByteOp) { |
| 4859 | op->type = OP_NONE; |
| 4860 | break; |
| 4861 | } |
| 4862 | op->type = OP_REG; |
| 4863 | op->bytes = ctxt->op_bytes; |
| 4864 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4865 | fetch_register_operand(op); |
| 4866 | op->orig_val = op->val; |
| 4867 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4868 | case OpDI: |
| 4869 | op->type = OP_MEM; |
| 4870 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4871 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4872 | register_address(ctxt, VCPU_REGS_RDI); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4873 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4874 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4875 | op->count = 1; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4876 | break; |
| 4877 | case OpDX: |
| 4878 | op->type = OP_REG; |
| 4879 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4880 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4881 | fetch_register_operand(op); |
| 4882 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4883 | case OpCL: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4884 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4885 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4886 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4887 | break; |
| 4888 | case OpImmByte: |
| 4889 | rc = decode_imm(ctxt, op, 1, true); |
| 4890 | break; |
| 4891 | case OpOne: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4892 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4893 | op->bytes = 1; |
| 4894 | op->val = 1; |
| 4895 | break; |
| 4896 | case OpImm: |
| 4897 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4898 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4899 | case OpImm64: |
| 4900 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4901 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4902 | case OpMem8: |
| 4903 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4904 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4905 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4906 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4907 | fetch_register_operand(&ctxt->memop); |
| 4908 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4909 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4910 | case OpMem16: |
| 4911 | ctxt->memop.bytes = 2; |
| 4912 | goto mem_common; |
| 4913 | case OpMem32: |
| 4914 | ctxt->memop.bytes = 4; |
| 4915 | goto mem_common; |
| 4916 | case OpImmU16: |
| 4917 | rc = decode_imm(ctxt, op, 2, false); |
| 4918 | break; |
| 4919 | case OpImmU: |
| 4920 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4921 | break; |
| 4922 | case OpSI: |
| 4923 | op->type = OP_MEM; |
| 4924 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4925 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4926 | register_address(ctxt, VCPU_REGS_RSI); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4927 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4928 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4929 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4930 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4931 | case OpXLat: |
| 4932 | op->type = OP_MEM; |
| 4933 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4934 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4935 | address_mask(ctxt, |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4936 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4937 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4938 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4939 | op->val = 0; |
| 4940 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4941 | case OpImmFAddr: |
| 4942 | op->type = OP_IMM; |
| 4943 | op->addr.mem.ea = ctxt->_eip; |
| 4944 | op->bytes = ctxt->op_bytes + 2; |
| 4945 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4946 | break; |
| 4947 | case OpMemFAddr: |
| 4948 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4949 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4950 | case OpES: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4951 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4952 | op->val = VCPU_SREG_ES; |
| 4953 | break; |
| 4954 | case OpCS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4955 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4956 | op->val = VCPU_SREG_CS; |
| 4957 | break; |
| 4958 | case OpSS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4959 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4960 | op->val = VCPU_SREG_SS; |
| 4961 | break; |
| 4962 | case OpDS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4963 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4964 | op->val = VCPU_SREG_DS; |
| 4965 | break; |
| 4966 | case OpFS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4967 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4968 | op->val = VCPU_SREG_FS; |
| 4969 | break; |
| 4970 | case OpGS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4971 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4972 | op->val = VCPU_SREG_GS; |
| 4973 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4974 | case OpImplicit: |
| 4975 | /* Special instructions do their own operand decoding. */ |
| 4976 | default: |
| 4977 | op->type = OP_NONE; /* Disable writeback. */ |
| 4978 | break; |
| 4979 | } |
| 4980 | |
| 4981 | done: |
| 4982 | return rc; |
| 4983 | } |
| 4984 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4985 | 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] | 4986 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4987 | int rc = X86EMUL_CONTINUE; |
| 4988 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4989 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4990 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4991 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4992 | struct opcode opcode; |
Wanpeng Li | ec73d16 | 2017-11-05 16:54:47 -0800 | [diff] [blame] | 4993 | u16 dummy; |
| 4994 | struct desc_struct desc; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4995 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4996 | ctxt->memop.type = OP_NONE; |
| 4997 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4998 | ctxt->_eip = ctxt->eip; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 4999 | ctxt->fetch.ptr = ctxt->fetch.data; |
| 5000 | ctxt->fetch.end = ctxt->fetch.data + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 5001 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5002 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5003 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 5004 | else { |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 5005 | rc = __do_insn_fetch_bytes(ctxt, 1); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 5006 | if (rc != X86EMUL_CONTINUE) |
| 5007 | return rc; |
| 5008 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5009 | |
| 5010 | switch (mode) { |
| 5011 | case X86EMUL_MODE_REAL: |
| 5012 | case X86EMUL_MODE_VM86: |
Wanpeng Li | ec73d16 | 2017-11-05 16:54:47 -0800 | [diff] [blame] | 5013 | def_op_bytes = def_ad_bytes = 2; |
| 5014 | ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS); |
| 5015 | if (desc.d) |
| 5016 | def_op_bytes = def_ad_bytes = 4; |
| 5017 | break; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5018 | case X86EMUL_MODE_PROT16: |
| 5019 | def_op_bytes = def_ad_bytes = 2; |
| 5020 | break; |
| 5021 | case X86EMUL_MODE_PROT32: |
| 5022 | def_op_bytes = def_ad_bytes = 4; |
| 5023 | break; |
| 5024 | #ifdef CONFIG_X86_64 |
| 5025 | case X86EMUL_MODE_PROT64: |
| 5026 | def_op_bytes = 4; |
| 5027 | def_ad_bytes = 8; |
| 5028 | break; |
| 5029 | #endif |
| 5030 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 5031 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5032 | } |
| 5033 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5034 | ctxt->op_bytes = def_op_bytes; |
| 5035 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5036 | |
| 5037 | /* Legacy prefixes. */ |
| 5038 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 5039 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5040 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 5041 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5042 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5043 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5044 | break; |
| 5045 | case 0x67: /* address-size override */ |
| 5046 | if (mode == X86EMUL_MODE_PROT64) |
| 5047 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5048 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5049 | else |
| 5050 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5051 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5052 | break; |
| 5053 | case 0x26: /* ES override */ |
| 5054 | case 0x2e: /* CS override */ |
| 5055 | case 0x36: /* SS override */ |
| 5056 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5057 | has_seg_override = true; |
| 5058 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5059 | break; |
| 5060 | case 0x64: /* FS override */ |
| 5061 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5062 | has_seg_override = true; |
| 5063 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5064 | break; |
| 5065 | case 0x40 ... 0x4f: /* REX */ |
| 5066 | if (mode != X86EMUL_MODE_PROT64) |
| 5067 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5068 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5069 | continue; |
| 5070 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5071 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5072 | break; |
| 5073 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5074 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5075 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5076 | break; |
| 5077 | default: |
| 5078 | goto done_prefixes; |
| 5079 | } |
| 5080 | |
| 5081 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 5082 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5083 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | done_prefixes: |
| 5087 | |
| 5088 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5089 | if (ctxt->rex_prefix & 8) |
| 5090 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5091 | |
| 5092 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5093 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 5094 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5095 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 5096 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 5097 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5098 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5099 | |
| 5100 | /* 0F_38 opcode map */ |
| 5101 | if (ctxt->b == 0x38) { |
| 5102 | ctxt->opcode_len = 3; |
| 5103 | ctxt->b = insn_fetch(u8, ctxt); |
| 5104 | opcode = opcode_map_0f_38[ctxt->b]; |
| 5105 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5106 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5107 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5108 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 5109 | if (ctxt->d & ModRM) |
| 5110 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 5111 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 5112 | /* vex-prefix instructions are not implemented */ |
| 5113 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
Nadav Amit | d14cb5d | 2014-11-02 11:54:58 +0200 | [diff] [blame] | 5114 | (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) { |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 5115 | ctxt->d = NotImpl; |
| 5116 | } |
| 5117 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5118 | while (ctxt->d & GroupMask) { |
| 5119 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5120 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5121 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5122 | opcode = opcode.u.group[goffset]; |
| 5123 | break; |
| 5124 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5125 | goffset = (ctxt->modrm >> 3) & 7; |
| 5126 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5127 | opcode = opcode.u.gdual->mod3[goffset]; |
| 5128 | else |
| 5129 | opcode = opcode.u.gdual->mod012[goffset]; |
| 5130 | break; |
| 5131 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5132 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 5133 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5134 | break; |
| 5135 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5136 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 5137 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5138 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5139 | switch (simd_prefix) { |
| 5140 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 5141 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 5142 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 5143 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 5144 | } |
| 5145 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 5146 | case Escape: |
| 5147 | if (ctxt->modrm > 0xbf) |
| 5148 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 5149 | else |
| 5150 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 5151 | break; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 5152 | case InstrDual: |
| 5153 | if ((ctxt->modrm >> 6) == 3) |
| 5154 | opcode = opcode.u.idual->mod3; |
| 5155 | else |
| 5156 | opcode = opcode.u.idual->mod012; |
| 5157 | break; |
Nadav Amit | 2276b51 | 2015-01-26 09:32:24 +0200 | [diff] [blame] | 5158 | case ModeDual: |
| 5159 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 5160 | opcode = opcode.u.mdual->mode64; |
| 5161 | else |
| 5162 | opcode = opcode.u.mdual->mode32; |
| 5163 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5164 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 5165 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 5166 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 5167 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 5168 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5169 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 5172 | /* Unrecognised? */ |
| 5173 | if (ctxt->d == 0) |
| 5174 | return EMULATION_FAILED; |
| 5175 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5176 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5177 | |
Nadav Amit | 3a6095a | 2014-08-13 16:50:13 +0300 | [diff] [blame] | 5178 | if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) |
| 5179 | return EMULATION_FAILED; |
| 5180 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5181 | if (unlikely(ctxt->d & |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 5182 | (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch| |
| 5183 | No16))) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5184 | /* |
| 5185 | * These are copied unconditionally here, and checked unconditionally |
| 5186 | * in x86_emulate_insn. |
| 5187 | */ |
| 5188 | ctxt->check_perm = opcode.check_perm; |
| 5189 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5190 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5191 | if (ctxt->d & NotImpl) |
| 5192 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 5193 | |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 5194 | if (mode == X86EMUL_MODE_PROT64) { |
| 5195 | if (ctxt->op_bytes == 4 && (ctxt->d & Stack)) |
| 5196 | ctxt->op_bytes = 8; |
| 5197 | else if (ctxt->d & NearBranch) |
| 5198 | ctxt->op_bytes = 8; |
| 5199 | } |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 5200 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5201 | if (ctxt->d & Op3264) { |
| 5202 | if (mode == X86EMUL_MODE_PROT64) |
| 5203 | ctxt->op_bytes = 8; |
| 5204 | else |
| 5205 | ctxt->op_bytes = 4; |
| 5206 | } |
| 5207 | |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 5208 | if ((ctxt->d & No16) && ctxt->op_bytes == 2) |
| 5209 | ctxt->op_bytes = 4; |
| 5210 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5211 | if (ctxt->d & Sse) |
| 5212 | ctxt->op_bytes = 16; |
| 5213 | else if (ctxt->d & Mmx) |
| 5214 | ctxt->op_bytes = 8; |
| 5215 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 5216 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5217 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5218 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 5219 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5220 | if (!has_seg_override) { |
| 5221 | has_seg_override = true; |
| 5222 | ctxt->seg_override = ctxt->modrm_seg; |
| 5223 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5224 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 5225 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5226 | if (rc != X86EMUL_CONTINUE) |
| 5227 | goto done; |
| 5228 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5229 | if (!has_seg_override) |
| 5230 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5231 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5232 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5233 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5234 | /* |
| 5235 | * Decode and fetch the source operand: register, memory |
| 5236 | * or immediate. |
| 5237 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 5238 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 5239 | if (rc != X86EMUL_CONTINUE) |
| 5240 | goto done; |
| 5241 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5242 | /* |
| 5243 | * Decode and fetch the second source operand: register, memory |
| 5244 | * or immediate. |
| 5245 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 5246 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 5247 | if (rc != X86EMUL_CONTINUE) |
| 5248 | goto done; |
| 5249 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5250 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 5251 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5252 | |
Owen Hofmann | d9092f5 | 2016-10-27 11:25:52 -0700 | [diff] [blame] | 5253 | if (ctxt->rip_relative && likely(ctxt->memopp)) |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 5254 | ctxt->memopp->addr.mem.ea = address_mask(ctxt, |
| 5255 | ctxt->memopp->addr.mem.ea + ctxt->_eip); |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 5256 | |
Paolo Bonzini | a430c91 | 2014-10-23 14:54:14 +0200 | [diff] [blame] | 5257 | done: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 5258 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 5259 | } |
| 5260 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 5261 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 5262 | { |
| 5263 | return ctxt->d & PageTable; |
| 5264 | } |
| 5265 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5266 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 5267 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5268 | /* The second termination condition only applies for REPE |
| 5269 | * and REPNE. Test if the repeat string operation prefix is |
| 5270 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 5271 | * corresponding termination condition according to: |
| 5272 | * - if REPE/REPZ and ZF = 0 then done |
| 5273 | * - if REPNE/REPNZ and ZF = 1 then done |
| 5274 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5275 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 5276 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 5277 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5278 | ((ctxt->eflags & X86_EFLAGS_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5279 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5280 | ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF)))) |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5281 | return true; |
| 5282 | |
| 5283 | return false; |
| 5284 | } |
| 5285 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5286 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 5287 | { |
Radim Krčmář | aae8f34 | 2016-11-08 20:54:18 +0100 | [diff] [blame] | 5288 | int rc; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5289 | |
| 5290 | ctxt->ops->get_fpu(ctxt); |
Radim Krčmář | aae8f34 | 2016-11-08 20:54:18 +0100 | [diff] [blame] | 5291 | rc = asm_safe("fwait"); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5292 | ctxt->ops->put_fpu(ctxt); |
| 5293 | |
Radim Krčmář | aae8f34 | 2016-11-08 20:54:18 +0100 | [diff] [blame] | 5294 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5295 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 5296 | |
| 5297 | return X86EMUL_CONTINUE; |
| 5298 | } |
| 5299 | |
| 5300 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 5301 | struct operand *op) |
| 5302 | { |
| 5303 | if (op->type == OP_MM) |
| 5304 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 5305 | } |
| 5306 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5307 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 5308 | { |
Josh Poimboeuf | 4548f63 | 2016-03-09 12:59:50 -0600 | [diff] [blame] | 5309 | register void *__sp asm(_ASM_SP); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5310 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Josh Poimboeuf | 4548f63 | 2016-03-09 12:59:50 -0600 | [diff] [blame] | 5311 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 5312 | if (!(ctxt->d & ByteOp)) |
| 5313 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Josh Poimboeuf | 4548f63 | 2016-03-09 12:59:50 -0600 | [diff] [blame] | 5314 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5315 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 5316 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
Josh Poimboeuf | 4548f63 | 2016-03-09 12:59:50 -0600 | [diff] [blame] | 5317 | [fastop]"+S"(fop), "+r"(__sp) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 5318 | : "c"(ctxt->src2.val)); |
Josh Poimboeuf | 4548f63 | 2016-03-09 12:59:50 -0600 | [diff] [blame] | 5319 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5320 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 5321 | if (!fop) /* exception is returned in fop variable */ |
| 5322 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5323 | return X86EMUL_CONTINUE; |
| 5324 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5325 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 5326 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 5327 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 5328 | memset(&ctxt->rip_relative, 0, |
| 5329 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 5330 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 5331 | ctxt->io_read.pos = 0; |
| 5332 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 5333 | ctxt->mem_read.end = 0; |
| 5334 | } |
| 5335 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 5336 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 5337 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 5338 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 5339 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5340 | int saved_dst_type = ctxt->dst.type; |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 5341 | unsigned emul_flags; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 5342 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5343 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 5344 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 5345 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5346 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 5347 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 5348 | goto done; |
| 5349 | } |
| 5350 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5351 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 5352 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 5353 | goto done; |
| 5354 | } |
| 5355 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 5356 | emul_flags = ctxt->ops->get_hflags(ctxt); |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5357 | if (unlikely(ctxt->d & |
| 5358 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 5359 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 5360 | (ctxt->d & Undefined)) { |
| 5361 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5362 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5363 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 5364 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5365 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 5366 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 5367 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 5368 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5369 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 5370 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5371 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 5372 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 5373 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5374 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 5375 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5376 | if (ctxt->d & Mmx) { |
| 5377 | rc = flush_pending_x87_faults(ctxt); |
| 5378 | if (rc != X86EMUL_CONTINUE) |
| 5379 | goto done; |
| 5380 | /* |
| 5381 | * Now that we know the fpu is exception safe, we can fetch |
| 5382 | * operands from it. |
| 5383 | */ |
| 5384 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 5385 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 5386 | if (!(ctxt->d & Mov)) |
| 5387 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 5388 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 5389 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 5390 | if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5391 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 5392 | X86_ICPT_PRE_EXCEPT); |
| 5393 | if (rc != X86EMUL_CONTINUE) |
| 5394 | goto done; |
| 5395 | } |
| 5396 | |
Nadav Amit | 64a3829 | 2014-12-10 11:19:04 +0200 | [diff] [blame] | 5397 | /* Instruction can only be executed in protected mode */ |
| 5398 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 5399 | rc = emulate_ud(ctxt); |
| 5400 | goto done; |
| 5401 | } |
| 5402 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5403 | /* Privileged instruction can be executed only in CPL=0 */ |
| 5404 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 5405 | if (ctxt->d & PrivUD) |
| 5406 | rc = emulate_ud(ctxt); |
| 5407 | else |
| 5408 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 5409 | goto done; |
| 5410 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5411 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5412 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 5413 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5414 | rc = ctxt->check_perm(ctxt); |
| 5415 | if (rc != X86EMUL_CONTINUE) |
| 5416 | goto done; |
| 5417 | } |
| 5418 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 5419 | if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5420 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 5421 | X86_ICPT_POST_EXCEPT); |
| 5422 | if (rc != X86EMUL_CONTINUE) |
| 5423 | goto done; |
| 5424 | } |
| 5425 | |
| 5426 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 5427 | /* All REP prefixes have the same first termination condition */ |
| 5428 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Nadav Amit | 428e3d0 | 2015-04-28 13:06:01 +0300 | [diff] [blame] | 5429 | string_registers_quirk(ctxt); |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5430 | ctxt->eip = ctxt->_eip; |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5431 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 5432 | goto done; |
| 5433 | } |
| 5434 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 5435 | } |
| 5436 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5437 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 5438 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 5439 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 5440 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 5441 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5442 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 5443 | } |
| 5444 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5445 | if (ctxt->src2.type == OP_MEM) { |
| 5446 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 5447 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 5448 | if (rc != X86EMUL_CONTINUE) |
| 5449 | goto done; |
| 5450 | } |
| 5451 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5452 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 5453 | goto special_insn; |
| 5454 | |
| 5455 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5456 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 5457 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5458 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 5459 | &ctxt->dst.val, ctxt->dst.bytes); |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 5460 | if (rc != X86EMUL_CONTINUE) { |
Paolo Bonzini | d44e121 | 2015-02-09 10:02:05 +0100 | [diff] [blame] | 5461 | if (!(ctxt->d & NoWrite) && |
| 5462 | rc == X86EMUL_PROPAGATE_FAULT && |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 5463 | ctxt->exception.vector == PF_VECTOR) |
| 5464 | ctxt->exception.error_code |= PFERR_WRITE_MASK; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 5465 | goto done; |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 5466 | } |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 5467 | } |
Paolo Bonzini | 4ff6f8e | 2015-02-12 17:04:47 +0100 | [diff] [blame] | 5468 | /* Copy full 64-bit value for CMPXCHG8B. */ |
| 5469 | ctxt->dst.orig_val64 = ctxt->dst.val64; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 5470 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5471 | special_insn: |
| 5472 | |
Ladi Prosek | 1eeb794 | 2017-04-25 16:42:44 +0200 | [diff] [blame] | 5473 | if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5474 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 5475 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 5476 | if (rc != X86EMUL_CONTINUE) |
| 5477 | goto done; |
| 5478 | } |
| 5479 | |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 5480 | if (ctxt->rep_prefix && (ctxt->d & String)) |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5481 | ctxt->eflags |= X86_EFLAGS_RF; |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 5482 | else |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5483 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 5484 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5485 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 5486 | if (ctxt->d & Fastop) { |
| 5487 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 5488 | rc = fastop(ctxt, fop); |
| 5489 | if (rc != X86EMUL_CONTINUE) |
| 5490 | goto done; |
| 5491 | goto writeback; |
| 5492 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5493 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 5494 | if (rc != X86EMUL_CONTINUE) |
| 5495 | goto done; |
| 5496 | goto writeback; |
| 5497 | } |
| 5498 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 5499 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5500 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5501 | else if (ctxt->opcode_len == 3) |
| 5502 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5503 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5504 | switch (ctxt->b) { |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5505 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5506 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5507 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5508 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 5509 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5510 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 5511 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 5512 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5513 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 5514 | ctxt->dst.type = OP_NONE; |
| 5515 | else |
| 5516 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 5517 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 5518 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5519 | switch (ctxt->op_bytes) { |
| 5520 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 5521 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 5522 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 5523 | } |
| 5524 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5525 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 5526 | rc = emulate_int(ctxt, 3); |
| 5527 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5528 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5529 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5530 | break; |
| 5531 | case 0xce: /* into */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5532 | if (ctxt->eflags & X86_EFLAGS_OF) |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 5533 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 5534 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 5535 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 5536 | case 0xeb: /* jmp rel short */ |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5537 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5538 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 5539 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5540 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 5541 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 5542 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5543 | case 0xf5: /* cmc */ |
| 5544 | /* complement carry flag from eflags reg */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5545 | ctxt->eflags ^= X86_EFLAGS_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5546 | break; |
| 5547 | case 0xf8: /* clc */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5548 | ctxt->eflags &= ~X86_EFLAGS_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5549 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 5550 | case 0xf9: /* stc */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5551 | ctxt->eflags |= X86_EFLAGS_CF; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 5552 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5553 | case 0xfc: /* cld */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5554 | ctxt->eflags &= ~X86_EFLAGS_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5555 | break; |
| 5556 | case 0xfd: /* std */ |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5557 | ctxt->eflags |= X86_EFLAGS_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5558 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5559 | default: |
| 5560 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5561 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5562 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5563 | if (rc != X86EMUL_CONTINUE) |
| 5564 | goto done; |
| 5565 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5566 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 5567 | if (ctxt->d & SrcWrite) { |
| 5568 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 5569 | rc = writeback(ctxt, &ctxt->src); |
| 5570 | if (rc != X86EMUL_CONTINUE) |
| 5571 | goto done; |
| 5572 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 5573 | if (!(ctxt->d & NoWrite)) { |
| 5574 | rc = writeback(ctxt, &ctxt->dst); |
| 5575 | if (rc != X86EMUL_CONTINUE) |
| 5576 | goto done; |
| 5577 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5578 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5579 | /* |
| 5580 | * restore dst type in case the decoding will be reused |
| 5581 | * (happens for string instruction ) |
| 5582 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5583 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5584 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5585 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5586 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 5587 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5588 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5589 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 5590 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5591 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5592 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5593 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5594 | if ((ctxt->d & SrcMask) == SrcSI) |
| 5595 | count = ctxt->src.count; |
| 5596 | else |
| 5597 | count = ctxt->dst.count; |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 5598 | register_address_increment(ctxt, VCPU_REGS_RCX, -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5599 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5600 | if (!string_insn_completed(ctxt)) { |
| 5601 | /* |
| 5602 | * Re-enter guest when pio read ahead buffer is empty |
| 5603 | * or, if it is not used, after each 1024 iteration. |
| 5604 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5605 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5606 | (r->end == 0 || r->end != r->pos)) { |
| 5607 | /* |
| 5608 | * Reset read cache. Usually happens before |
| 5609 | * decode, but since instruction is restarted |
| 5610 | * we have to do it here. |
| 5611 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5612 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5613 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5614 | return EMULATION_RESTART; |
| 5615 | } |
| 5616 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 5617 | } |
Nadav Amit | 0efb044 | 2015-03-29 16:33:03 +0300 | [diff] [blame] | 5618 | ctxt->eflags &= ~X86_EFLAGS_RF; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5619 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5620 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5621 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5622 | |
| 5623 | done: |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5624 | if (rc == X86EMUL_PROPAGATE_FAULT) { |
| 5625 | WARN_ON(ctxt->exception.vector > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 5626 | ctxt->have_exception = true; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5627 | } |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 5628 | if (rc == X86EMUL_INTERCEPTED) |
| 5629 | return EMULATION_INTERCEPTED; |
| 5630 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5631 | if (rc == X86EMUL_CONTINUE) |
| 5632 | writeback_registers(ctxt); |
| 5633 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5634 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5635 | |
| 5636 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5637 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5638 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 5639 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5640 | break; |
| 5641 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5642 | case 0x0d: /* GrpP (prefetch) */ |
| 5643 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 5644 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5645 | break; |
| 5646 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5647 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5648 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5649 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5650 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5651 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5652 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 5653 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 5654 | ctxt->dst.val = ctxt->src.val; |
Nadav Amit | b91aa14 | 2015-03-30 15:39:19 +0300 | [diff] [blame] | 5655 | else if (ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5656 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5657 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5658 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5659 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5660 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5661 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5662 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5663 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5664 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5665 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5666 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5667 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5668 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5669 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5670 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5671 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5672 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5673 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5674 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5675 | default: |
| 5676 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5677 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5678 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5679 | threebyte_insn: |
| 5680 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5681 | if (rc != X86EMUL_CONTINUE) |
| 5682 | goto done; |
| 5683 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5684 | goto writeback; |
| 5685 | |
| 5686 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 5687 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5688 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5689 | |
| 5690 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5691 | { |
| 5692 | invalidate_registers(ctxt); |
| 5693 | } |
| 5694 | |
| 5695 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5696 | { |
| 5697 | writeback_registers(ctxt); |
| 5698 | } |