Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 29 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 30 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | /* |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 89 | #define DstMem16 (OpMem16 << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 90 | #define DstImmUByte (OpImmUByte << DstShift) |
| 91 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 92 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 93 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 94 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 95 | #define SrcShift 6 |
| 96 | #define SrcNone (OpNone << SrcShift) |
| 97 | #define SrcReg (OpReg << SrcShift) |
| 98 | #define SrcMem (OpMem << SrcShift) |
| 99 | #define SrcMem16 (OpMem16 << SrcShift) |
| 100 | #define SrcMem32 (OpMem32 << SrcShift) |
| 101 | #define SrcImm (OpImm << SrcShift) |
| 102 | #define SrcImmByte (OpImmByte << SrcShift) |
| 103 | #define SrcOne (OpOne << SrcShift) |
| 104 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 105 | #define SrcImmU (OpImmU << SrcShift) |
| 106 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 107 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 108 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 109 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 110 | #define SrcAcc (OpAcc << SrcShift) |
| 111 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 112 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 113 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 114 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 115 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 116 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 117 | #define BitOp (1<<11) |
| 118 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 119 | #define String (1<<13) /* String instruction (rep capable) */ |
| 120 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 121 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 122 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 123 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 124 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 125 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 126 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 127 | #define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 128 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 129 | /* Generic ModRM decode. */ |
| 130 | #define ModRM (1<<19) |
| 131 | /* Destination is only written; never read. */ |
| 132 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 133 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 134 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 135 | #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 136 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 137 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 138 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 139 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 140 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 141 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 142 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 143 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 144 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 145 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 146 | #define Src2None (OpNone << Src2Shift) |
Avi Kivity | ab2c5ce | 2013-02-09 11:31:46 +0200 | [diff] [blame] | 147 | #define Src2Mem (OpMem << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 148 | #define Src2CL (OpCL << Src2Shift) |
| 149 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 150 | #define Src2One (OpOne << Src2Shift) |
| 151 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 152 | #define Src2ES (OpES << Src2Shift) |
| 153 | #define Src2CS (OpCS << Src2Shift) |
| 154 | #define Src2SS (OpSS << Src2Shift) |
| 155 | #define Src2DS (OpDS << Src2Shift) |
| 156 | #define Src2FS (OpFS << Src2Shift) |
| 157 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 158 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 159 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 160 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 161 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 162 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 163 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 164 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 165 | #define SrcWrite ((u64)1 << 46) /* Write back src operand */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 166 | #define NoMod ((u64)1 << 47) /* Mod field is ignored */ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 167 | #define Intercept ((u64)1 << 48) /* Has valid intercept field */ |
| 168 | #define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */ |
Nadav Amit | 10e38fc | 2014-06-18 17:19:34 +0300 | [diff] [blame] | 169 | #define NoBigReal ((u64)1 << 50) /* No big real mode */ |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 170 | #define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 171 | #define NearBranch ((u64)1 << 52) /* Near branches */ |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 172 | #define No16 ((u64)1 << 53) /* No 16 bit operand */ |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 173 | #define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 175 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 176 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 177 | #define X2(x...) x, x |
| 178 | #define X3(x...) X2(x), x |
| 179 | #define X4(x...) X2(x), X2(x) |
| 180 | #define X5(x...) X4(x), x |
| 181 | #define X6(x...) X4(x), X2(x) |
| 182 | #define X7(x...) X4(x), X3(x) |
| 183 | #define X8(x...) X4(x), X4(x) |
| 184 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 185 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 186 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 187 | #define FASTOP_SIZE 8 |
| 188 | |
| 189 | /* |
| 190 | * fastop functions have a special calling convention: |
| 191 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 192 | * dst: rax (in/out) |
| 193 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 194 | * src2: rcx (in) |
| 195 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 196 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 197 | * |
| 198 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 199 | * different operand sizes can be reached by calculation, rather than a jump |
| 200 | * table (which would be bigger than the code). |
| 201 | * |
| 202 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 203 | * so they can't be called from C directly. |
| 204 | */ |
| 205 | |
| 206 | struct fastop; |
| 207 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 208 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 209 | u64 flags : 56; |
| 210 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 211 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 212 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 213 | const struct opcode *group; |
| 214 | const struct group_dual *gdual; |
| 215 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 216 | const struct escape *esc; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 217 | const struct instr_dual *idual; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 218 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 219 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 220 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | struct group_dual { |
| 224 | struct opcode mod012[8]; |
| 225 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 226 | }; |
| 227 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 228 | struct gprefix { |
| 229 | struct opcode pfx_no; |
| 230 | struct opcode pfx_66; |
| 231 | struct opcode pfx_f2; |
| 232 | struct opcode pfx_f3; |
| 233 | }; |
| 234 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 235 | struct escape { |
| 236 | struct opcode op[8]; |
| 237 | struct opcode high[64]; |
| 238 | }; |
| 239 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 240 | struct instr_dual { |
| 241 | struct opcode mod012; |
| 242 | struct opcode mod3; |
| 243 | }; |
| 244 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 245 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 246 | #define EFLG_ID (1<<21) |
| 247 | #define EFLG_VIP (1<<20) |
| 248 | #define EFLG_VIF (1<<19) |
| 249 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 250 | #define EFLG_VM (1<<17) |
| 251 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 252 | #define EFLG_IOPL (3<<12) |
| 253 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 254 | #define EFLG_OF (1<<11) |
| 255 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 256 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 257 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 258 | #define EFLG_SF (1<<7) |
| 259 | #define EFLG_ZF (1<<6) |
| 260 | #define EFLG_AF (1<<4) |
| 261 | #define EFLG_PF (1<<2) |
| 262 | #define EFLG_CF (1<<0) |
| 263 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 264 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 265 | #define EFLG_RESERVED_ONE_MASK 2 |
| 266 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 267 | enum x86_transfer_type { |
| 268 | X86_TRANSFER_NONE, |
| 269 | X86_TRANSFER_CALL_JMP, |
| 270 | X86_TRANSFER_RET, |
| 271 | X86_TRANSFER_TASK_SWITCH, |
| 272 | }; |
| 273 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 274 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 275 | { |
| 276 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 277 | ctxt->regs_valid |= 1 << nr; |
| 278 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 279 | } |
| 280 | return ctxt->_regs[nr]; |
| 281 | } |
| 282 | |
| 283 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 284 | { |
| 285 | ctxt->regs_valid |= 1 << nr; |
| 286 | ctxt->regs_dirty |= 1 << nr; |
| 287 | return &ctxt->_regs[nr]; |
| 288 | } |
| 289 | |
| 290 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 291 | { |
| 292 | reg_read(ctxt, nr); |
| 293 | return reg_write(ctxt, nr); |
| 294 | } |
| 295 | |
| 296 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 297 | { |
| 298 | unsigned reg; |
| 299 | |
| 300 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 301 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 302 | } |
| 303 | |
| 304 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 305 | { |
| 306 | ctxt->regs_dirty = 0; |
| 307 | ctxt->regs_valid = 0; |
| 308 | } |
| 309 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 310 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 311 | * These EFLAGS bits are restored from saved value during emulation, and |
| 312 | * any changes are written back to the saved value after emulation. |
| 313 | */ |
| 314 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 315 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 316 | #ifdef CONFIG_X86_64 |
| 317 | #define ON64(x) x |
| 318 | #else |
| 319 | #define ON64(x) |
| 320 | #endif |
| 321 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 322 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 323 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 324 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 325 | #define FOP_RET "ret \n\t" |
| 326 | |
| 327 | #define FOP_START(op) \ |
| 328 | extern void em_##op(struct fastop *fake); \ |
| 329 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 330 | ".global em_" #op " \n\t" \ |
| 331 | FOP_ALIGN \ |
| 332 | "em_" #op ": \n\t" |
| 333 | |
| 334 | #define FOP_END \ |
| 335 | ".popsection") |
| 336 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 337 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 338 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 339 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 340 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 341 | |
| 342 | #define FOP1EEX(op, dst) \ |
| 343 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 344 | |
| 345 | #define FASTOP1(op) \ |
| 346 | FOP_START(op) \ |
| 347 | FOP1E(op##b, al) \ |
| 348 | FOP1E(op##w, ax) \ |
| 349 | FOP1E(op##l, eax) \ |
| 350 | ON64(FOP1E(op##q, rax)) \ |
| 351 | FOP_END |
| 352 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 353 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 354 | #define FASTOP1SRC2(op, name) \ |
| 355 | FOP_START(name) \ |
| 356 | FOP1E(op, cl) \ |
| 357 | FOP1E(op, cx) \ |
| 358 | FOP1E(op, ecx) \ |
| 359 | ON64(FOP1E(op, rcx)) \ |
| 360 | FOP_END |
| 361 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 362 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 363 | #define FASTOP1SRC2EX(op, name) \ |
| 364 | FOP_START(name) \ |
| 365 | FOP1EEX(op, cl) \ |
| 366 | FOP1EEX(op, cx) \ |
| 367 | FOP1EEX(op, ecx) \ |
| 368 | ON64(FOP1EEX(op, rcx)) \ |
| 369 | FOP_END |
| 370 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 371 | #define FOP2E(op, dst, src) \ |
| 372 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 373 | |
| 374 | #define FASTOP2(op) \ |
| 375 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 376 | FOP2E(op##b, al, dl) \ |
| 377 | FOP2E(op##w, ax, dx) \ |
| 378 | FOP2E(op##l, eax, edx) \ |
| 379 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 380 | FOP_END |
| 381 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 382 | /* 2 operand, word only */ |
| 383 | #define FASTOP2W(op) \ |
| 384 | FOP_START(op) \ |
| 385 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 386 | FOP2E(op##w, ax, dx) \ |
| 387 | FOP2E(op##l, eax, edx) \ |
| 388 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 389 | FOP_END |
| 390 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 391 | /* 2 operand, src is CL */ |
| 392 | #define FASTOP2CL(op) \ |
| 393 | FOP_START(op) \ |
| 394 | FOP2E(op##b, al, cl) \ |
| 395 | FOP2E(op##w, ax, cl) \ |
| 396 | FOP2E(op##l, eax, cl) \ |
| 397 | ON64(FOP2E(op##q, rax, cl)) \ |
| 398 | FOP_END |
| 399 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 400 | /* 2 operand, src and dest are reversed */ |
| 401 | #define FASTOP2R(op, name) \ |
| 402 | FOP_START(name) \ |
| 403 | FOP2E(op##b, dl, al) \ |
| 404 | FOP2E(op##w, dx, ax) \ |
| 405 | FOP2E(op##l, edx, eax) \ |
| 406 | ON64(FOP2E(op##q, rdx, rax)) \ |
| 407 | FOP_END |
| 408 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 409 | #define FOP3E(op, dst, src, src2) \ |
| 410 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 411 | |
| 412 | /* 3-operand, word-only, src2=cl */ |
| 413 | #define FASTOP3WCL(op) \ |
| 414 | FOP_START(op) \ |
| 415 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 416 | FOP3E(op##w, ax, dx, cl) \ |
| 417 | FOP3E(op##l, eax, edx, cl) \ |
| 418 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 419 | FOP_END |
| 420 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 421 | /* Special case for SETcc - 1 instruction per cc */ |
| 422 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 423 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 424 | asm(".global kvm_fastop_exception \n" |
| 425 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 426 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 427 | FOP_START(setcc) |
| 428 | FOP_SETCC(seto) |
| 429 | FOP_SETCC(setno) |
| 430 | FOP_SETCC(setc) |
| 431 | FOP_SETCC(setnc) |
| 432 | FOP_SETCC(setz) |
| 433 | FOP_SETCC(setnz) |
| 434 | FOP_SETCC(setbe) |
| 435 | FOP_SETCC(setnbe) |
| 436 | FOP_SETCC(sets) |
| 437 | FOP_SETCC(setns) |
| 438 | FOP_SETCC(setp) |
| 439 | FOP_SETCC(setnp) |
| 440 | FOP_SETCC(setl) |
| 441 | FOP_SETCC(setnl) |
| 442 | FOP_SETCC(setle) |
| 443 | FOP_SETCC(setnle) |
| 444 | FOP_END; |
| 445 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 446 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 447 | FOP_END; |
| 448 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 449 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 450 | enum x86_intercept intercept, |
| 451 | enum x86_intercept_stage stage) |
| 452 | { |
| 453 | struct x86_instruction_info info = { |
| 454 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 455 | .rep_prefix = ctxt->rep_prefix, |
| 456 | .modrm_mod = ctxt->modrm_mod, |
| 457 | .modrm_reg = ctxt->modrm_reg, |
| 458 | .modrm_rm = ctxt->modrm_rm, |
| 459 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 460 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 461 | .src_bytes = ctxt->src.bytes, |
| 462 | .dst_bytes = ctxt->dst.bytes, |
| 463 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 464 | .next_rip = ctxt->eip, |
| 465 | }; |
| 466 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 467 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 470 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 471 | { |
| 472 | *dest = (*dest & ~mask) | (src & mask); |
| 473 | } |
| 474 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 475 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 476 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 477 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 480 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 481 | { |
| 482 | u16 sel; |
| 483 | struct desc_struct ss; |
| 484 | |
| 485 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 486 | return ~0UL; |
| 487 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 488 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 489 | } |
| 490 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 491 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 492 | { |
| 493 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 494 | } |
| 495 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 496 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 497 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 498 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 499 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 500 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 501 | return reg; |
| 502 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 503 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static inline unsigned long |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 507 | register_address(struct x86_emulate_ctxt *ctxt, int reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 508 | { |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 509 | return address_mask(ctxt, reg_read(ctxt, reg)); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 512 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 513 | { |
| 514 | assign_masked(reg, *reg + inc, mask); |
| 515 | } |
| 516 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 517 | static inline void |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 518 | register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 519 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 520 | ulong mask; |
| 521 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 522 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 523 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 524 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 525 | mask = ad_mask(ctxt); |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 526 | masked_increment(reg_rmw(ctxt, reg), mask, inc); |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 530 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 531 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 532 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 533 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 534 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 535 | { |
| 536 | u32 limit = get_desc_limit(desc); |
| 537 | |
| 538 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 539 | } |
| 540 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 541 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 542 | { |
| 543 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 544 | return 0; |
| 545 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 546 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 547 | } |
| 548 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 549 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 550 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 551 | { |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 552 | WARN_ON(vec > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 553 | ctxt->exception.vector = vec; |
| 554 | ctxt->exception.error_code = error; |
| 555 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 556 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 557 | } |
| 558 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 559 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 560 | { |
| 561 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 562 | } |
| 563 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 564 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 565 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 566 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 567 | } |
| 568 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 569 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 570 | { |
| 571 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 572 | } |
| 573 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 574 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 575 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 576 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 577 | } |
| 578 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 579 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 580 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 581 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 582 | } |
| 583 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 584 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 585 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 586 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 587 | } |
| 588 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 589 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 590 | { |
| 591 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 592 | } |
| 593 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 594 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 595 | { |
| 596 | u16 selector; |
| 597 | struct desc_struct desc; |
| 598 | |
| 599 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 600 | return selector; |
| 601 | } |
| 602 | |
| 603 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 604 | unsigned seg) |
| 605 | { |
| 606 | u16 dummy; |
| 607 | u32 base3; |
| 608 | struct desc_struct desc; |
| 609 | |
| 610 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 611 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 612 | } |
| 613 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 614 | /* |
| 615 | * x86 defines three classes of vector instructions: explicitly |
| 616 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 617 | * depending on whether they're AVX encoded or not. |
| 618 | * |
| 619 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 620 | * subject to the same check. |
| 621 | */ |
| 622 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 623 | { |
| 624 | if (likely(size < 16)) |
| 625 | return false; |
| 626 | |
| 627 | if (ctxt->d & Aligned) |
| 628 | return true; |
| 629 | else if (ctxt->d & Unaligned) |
| 630 | return false; |
| 631 | else if (ctxt->d & Avx) |
| 632 | return false; |
| 633 | else |
| 634 | return true; |
| 635 | } |
| 636 | |
Paolo Bonzini | d09155d | 2014-10-27 14:54:44 +0100 | [diff] [blame] | 637 | static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, |
| 638 | struct segmented_address addr, |
| 639 | unsigned *max_size, unsigned size, |
| 640 | bool write, bool fetch, |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 641 | enum x86emul_mode mode, ulong *linear) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 642 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 643 | struct desc_struct desc; |
| 644 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 645 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 646 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 647 | u16 sel; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 648 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 649 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 650 | *max_size = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 651 | switch (mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 652 | case X86EMUL_MODE_PROT64: |
Nadav Amit | 4be4de7 | 2014-09-18 22:39:40 +0300 | [diff] [blame] | 653 | if (is_noncanonical_address(la)) |
Nadav Amit | abc7d8a | 2014-11-19 17:43:12 +0200 | [diff] [blame] | 654 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 655 | |
| 656 | *max_size = min_t(u64, ~0u, (1ull << 48) - la); |
| 657 | if (size > *max_size) |
| 658 | goto bad; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 659 | break; |
| 660 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 661 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 662 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 663 | if (!usable) |
| 664 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 665 | /* code segment in protected mode or read-only data segment */ |
| 666 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 667 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 668 | goto bad; |
| 669 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 670 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 671 | goto bad; |
| 672 | lim = desc_limit_scaled(&desc); |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 673 | if (!(desc.type & 8) && (desc.type & 4)) { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 674 | /* expand-down segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 675 | if (addr.ea <= lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 676 | goto bad; |
| 677 | lim = desc.d ? 0xffffffff : 0xffff; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 678 | } |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 679 | if (addr.ea > lim) |
| 680 | goto bad; |
| 681 | *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea); |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 682 | if (size > *max_size) |
| 683 | goto bad; |
Nadav Amit | 31ff648 | 2014-11-19 17:43:13 +0200 | [diff] [blame] | 684 | la &= (u32)-1; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 685 | break; |
| 686 | } |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 687 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 688 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 689 | *linear = la; |
| 690 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 691 | bad: |
| 692 | if (addr.seg == VCPU_SREG_SS) |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 693 | return emulate_ss(ctxt, 0); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 694 | else |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 695 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 696 | } |
| 697 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 698 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 699 | struct segmented_address addr, |
| 700 | unsigned size, bool write, |
| 701 | ulong *linear) |
| 702 | { |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 703 | unsigned max_size; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 704 | return __linearize(ctxt, addr, &max_size, size, write, false, |
| 705 | ctxt->mode, linear); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 708 | static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 709 | enum x86emul_mode mode) |
| 710 | { |
| 711 | ulong linear; |
| 712 | int rc; |
| 713 | unsigned max_size; |
| 714 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 715 | .ea = dst }; |
| 716 | |
| 717 | if (ctxt->op_bytes != sizeof(unsigned long)) |
| 718 | addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1); |
| 719 | rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear); |
| 720 | if (rc == X86EMUL_CONTINUE) |
| 721 | ctxt->_eip = addr.ea; |
| 722 | return rc; |
| 723 | } |
| 724 | |
| 725 | static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst) |
| 726 | { |
| 727 | return assign_eip(ctxt, dst, ctxt->mode); |
| 728 | } |
| 729 | |
| 730 | static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 731 | const struct desc_struct *cs_desc) |
| 732 | { |
| 733 | enum x86emul_mode mode = ctxt->mode; |
| 734 | |
| 735 | #ifdef CONFIG_X86_64 |
| 736 | if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) { |
| 737 | u64 efer = 0; |
| 738 | |
| 739 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 740 | if (efer & EFER_LMA) |
| 741 | mode = X86EMUL_MODE_PROT64; |
| 742 | } |
| 743 | #endif |
| 744 | if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32) |
| 745 | mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 746 | return assign_eip(ctxt, dst, mode); |
| 747 | } |
| 748 | |
| 749 | static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
| 750 | { |
| 751 | return assign_eip_near(ctxt, ctxt->_eip + rel); |
| 752 | } |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 753 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 754 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 755 | struct segmented_address addr, |
| 756 | void *data, |
| 757 | unsigned size) |
| 758 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 759 | int rc; |
| 760 | ulong linear; |
| 761 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 762 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 763 | if (rc != X86EMUL_CONTINUE) |
| 764 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 765 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 768 | /* |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 769 | * Prefetch the remaining bytes of the instruction without crossing page |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 770 | * boundary if they are not in fetch_cache yet. |
| 771 | */ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 772 | 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] | 773 | { |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 774 | int rc; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 775 | unsigned size, max_size; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 776 | unsigned long linear; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 777 | int cur_size = ctxt->fetch.end - ctxt->fetch.data; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 778 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 779 | .ea = ctxt->eip + cur_size }; |
| 780 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 781 | /* |
| 782 | * We do not know exactly how many bytes will be needed, and |
| 783 | * __linearize is expensive, so fetch as much as possible. We |
| 784 | * just have to avoid going beyond the 15 byte limit, the end |
| 785 | * of the segment, or the end of the page. |
| 786 | * |
| 787 | * __linearize is called with size 0 so that it does not do any |
| 788 | * boundary check itself. Instead, we use max_size to check |
| 789 | * against op_size. |
| 790 | */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 791 | rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode, |
| 792 | &linear); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 793 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 794 | return rc; |
| 795 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 796 | size = min_t(unsigned, 15UL ^ cur_size, max_size); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 797 | size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear)); |
Paolo Bonzini | 5cfc7e0 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * One instruction can only straddle two pages, |
| 801 | * and one has been loaded at the beginning of |
| 802 | * x86_decode_insn. So, if not enough bytes |
| 803 | * still, we must have hit the 15-byte boundary. |
| 804 | */ |
| 805 | if (unlikely(size < op_size)) |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 806 | return emulate_gp(ctxt, 0); |
| 807 | |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 808 | rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 809 | size, &ctxt->exception); |
| 810 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 811 | return rc; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 812 | ctxt->fetch.end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 813 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 816 | static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, |
| 817 | unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 818 | { |
Nadav Amit | 08da44a | 2014-10-03 01:10:04 +0300 | [diff] [blame] | 819 | unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr; |
| 820 | |
| 821 | if (unlikely(done_size < size)) |
| 822 | return __do_insn_fetch_bytes(ctxt, size - done_size); |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 823 | else |
| 824 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 827 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 828 | #define insn_fetch(_type, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 829 | ({ _type _x; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 830 | \ |
| 831 | rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 832 | if (rc != X86EMUL_CONTINUE) \ |
| 833 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 834 | ctxt->_eip += sizeof(_type); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 835 | _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \ |
| 836 | ctxt->fetch.ptr += sizeof(_type); \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 837 | _x; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 838 | }) |
| 839 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 840 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 841 | ({ \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 842 | rc = do_insn_fetch_bytes(_ctxt, _size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 843 | if (rc != X86EMUL_CONTINUE) \ |
| 844 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 845 | ctxt->_eip += (_size); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 846 | memcpy(_arr, ctxt->fetch.ptr, _size); \ |
| 847 | ctxt->fetch.ptr += (_size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 848 | }) |
| 849 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 850 | /* |
| 851 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 852 | * pointer into the block that addresses the relevant register. |
| 853 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 854 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 855 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 856 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 857 | { |
| 858 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 859 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 860 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 861 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 862 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 863 | else |
| 864 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 865 | return p; |
| 866 | } |
| 867 | |
| 868 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 869 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 870 | u16 *size, unsigned long *address, int op_bytes) |
| 871 | { |
| 872 | int rc; |
| 873 | |
| 874 | if (op_bytes == 2) |
| 875 | op_bytes = 3; |
| 876 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 877 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 878 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 879 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 880 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 881 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 882 | return rc; |
| 883 | } |
| 884 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 885 | FASTOP2(add); |
| 886 | FASTOP2(or); |
| 887 | FASTOP2(adc); |
| 888 | FASTOP2(sbb); |
| 889 | FASTOP2(and); |
| 890 | FASTOP2(sub); |
| 891 | FASTOP2(xor); |
| 892 | FASTOP2(cmp); |
| 893 | FASTOP2(test); |
| 894 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 895 | FASTOP1SRC2(mul, mul_ex); |
| 896 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 897 | FASTOP1SRC2EX(div, div_ex); |
| 898 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 899 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 900 | FASTOP3WCL(shld); |
| 901 | FASTOP3WCL(shrd); |
| 902 | |
| 903 | FASTOP2W(imul); |
| 904 | |
| 905 | FASTOP1(not); |
| 906 | FASTOP1(neg); |
| 907 | FASTOP1(inc); |
| 908 | FASTOP1(dec); |
| 909 | |
| 910 | FASTOP2CL(rol); |
| 911 | FASTOP2CL(ror); |
| 912 | FASTOP2CL(rcl); |
| 913 | FASTOP2CL(rcr); |
| 914 | FASTOP2CL(shl); |
| 915 | FASTOP2CL(shr); |
| 916 | FASTOP2CL(sar); |
| 917 | |
| 918 | FASTOP2W(bsf); |
| 919 | FASTOP2W(bsr); |
| 920 | FASTOP2W(bt); |
| 921 | FASTOP2W(bts); |
| 922 | FASTOP2W(btr); |
| 923 | FASTOP2W(btc); |
| 924 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 925 | FASTOP2(xadd); |
| 926 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 927 | FASTOP2R(cmp, cmp_r); |
| 928 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 929 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 930 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 931 | u8 rc; |
| 932 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 933 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 934 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 935 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 936 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 937 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 938 | } |
| 939 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 940 | static void fetch_register_operand(struct operand *op) |
| 941 | { |
| 942 | switch (op->bytes) { |
| 943 | case 1: |
| 944 | op->val = *(u8 *)op->addr.reg; |
| 945 | break; |
| 946 | case 2: |
| 947 | op->val = *(u16 *)op->addr.reg; |
| 948 | break; |
| 949 | case 4: |
| 950 | op->val = *(u32 *)op->addr.reg; |
| 951 | break; |
| 952 | case 8: |
| 953 | op->val = *(u64 *)op->addr.reg; |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 958 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 959 | { |
| 960 | ctxt->ops->get_fpu(ctxt); |
| 961 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 962 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 963 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 964 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 965 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 966 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 967 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 968 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 969 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 970 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 971 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 972 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 973 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 974 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 975 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 976 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 977 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 978 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 979 | #endif |
| 980 | default: BUG(); |
| 981 | } |
| 982 | ctxt->ops->put_fpu(ctxt); |
| 983 | } |
| 984 | |
| 985 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 986 | int reg) |
| 987 | { |
| 988 | ctxt->ops->get_fpu(ctxt); |
| 989 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 990 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 991 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 992 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 993 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 994 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 995 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 996 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 997 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 998 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 999 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 1000 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1001 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1002 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1003 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1004 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1005 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1006 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1007 | #endif |
| 1008 | default: BUG(); |
| 1009 | } |
| 1010 | ctxt->ops->put_fpu(ctxt); |
| 1011 | } |
| 1012 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1013 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1014 | { |
| 1015 | ctxt->ops->get_fpu(ctxt); |
| 1016 | switch (reg) { |
| 1017 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1018 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1019 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1020 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1021 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1022 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1023 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1024 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1025 | default: BUG(); |
| 1026 | } |
| 1027 | ctxt->ops->put_fpu(ctxt); |
| 1028 | } |
| 1029 | |
| 1030 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1031 | { |
| 1032 | ctxt->ops->get_fpu(ctxt); |
| 1033 | switch (reg) { |
| 1034 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1035 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1036 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1037 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1038 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1039 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1040 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1041 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1042 | default: BUG(); |
| 1043 | } |
| 1044 | ctxt->ops->put_fpu(ctxt); |
| 1045 | } |
| 1046 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1047 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1048 | { |
| 1049 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1050 | return emulate_nm(ctxt); |
| 1051 | |
| 1052 | ctxt->ops->get_fpu(ctxt); |
| 1053 | asm volatile("fninit"); |
| 1054 | ctxt->ops->put_fpu(ctxt); |
| 1055 | return X86EMUL_CONTINUE; |
| 1056 | } |
| 1057 | |
| 1058 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1059 | { |
| 1060 | u16 fcw; |
| 1061 | |
| 1062 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1063 | return emulate_nm(ctxt); |
| 1064 | |
| 1065 | ctxt->ops->get_fpu(ctxt); |
| 1066 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1067 | ctxt->ops->put_fpu(ctxt); |
| 1068 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1069 | ctxt->dst.val = fcw; |
| 1070 | |
| 1071 | return X86EMUL_CONTINUE; |
| 1072 | } |
| 1073 | |
| 1074 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1075 | { |
| 1076 | u16 fsw; |
| 1077 | |
| 1078 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1079 | return emulate_nm(ctxt); |
| 1080 | |
| 1081 | ctxt->ops->get_fpu(ctxt); |
| 1082 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1083 | ctxt->ops->put_fpu(ctxt); |
| 1084 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1085 | ctxt->dst.val = fsw; |
| 1086 | |
| 1087 | return X86EMUL_CONTINUE; |
| 1088 | } |
| 1089 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1090 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1091 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1092 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1093 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1094 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1095 | if (!(ctxt->d & ModRM)) |
| 1096 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1097 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1098 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1099 | op->type = OP_XMM; |
| 1100 | op->bytes = 16; |
| 1101 | op->addr.xmm = reg; |
| 1102 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1103 | return; |
| 1104 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1105 | if (ctxt->d & Mmx) { |
| 1106 | reg &= 7; |
| 1107 | op->type = OP_MM; |
| 1108 | op->bytes = 8; |
| 1109 | op->addr.mm = reg; |
| 1110 | return; |
| 1111 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1112 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1113 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1114 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1115 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1116 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1117 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1118 | op->orig_val = op->val; |
| 1119 | } |
| 1120 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1121 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1122 | { |
| 1123 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1124 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1125 | } |
| 1126 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1127 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1128 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1129 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1130 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1131 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1132 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1133 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1134 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1135 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1136 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1137 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1138 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1139 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1140 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1141 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1142 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1143 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1144 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1145 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1146 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1147 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1148 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1149 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1150 | op->type = OP_XMM; |
| 1151 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1152 | op->addr.xmm = ctxt->modrm_rm; |
| 1153 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1154 | return rc; |
| 1155 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1156 | if (ctxt->d & Mmx) { |
| 1157 | op->type = OP_MM; |
| 1158 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1159 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1160 | return rc; |
| 1161 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1162 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1163 | return rc; |
| 1164 | } |
| 1165 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1166 | op->type = OP_MEM; |
| 1167 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1168 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1169 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1170 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1171 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1172 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1173 | |
| 1174 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1175 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1176 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1177 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1178 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1179 | break; |
| 1180 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1181 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1182 | break; |
| 1183 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1184 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1185 | break; |
| 1186 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1187 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1188 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1189 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1190 | break; |
| 1191 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1192 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1193 | break; |
| 1194 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1195 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1196 | break; |
| 1197 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1198 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1199 | break; |
| 1200 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1201 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1204 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1205 | break; |
| 1206 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1207 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1208 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1209 | break; |
| 1210 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1211 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1214 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1215 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1216 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1217 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1218 | } else { |
| 1219 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1220 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1221 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1222 | index_reg |= (sib >> 3) & 7; |
| 1223 | base_reg |= sib & 7; |
| 1224 | scale = sib >> 6; |
| 1225 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1226 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1227 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1228 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1229 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1230 | adjust_modrm_seg(ctxt, base_reg); |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 1231 | /* Increment ESP on POP [ESP] */ |
| 1232 | if ((ctxt->d & IncSP) && |
| 1233 | base_reg == VCPU_REGS_RSP) |
| 1234 | modrm_ea += ctxt->op_bytes; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1235 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1236 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1237 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1238 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Nadav Amit | 5b38ab8 | 2014-11-02 11:54:41 +0200 | [diff] [blame] | 1239 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1240 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1241 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1242 | } else { |
| 1243 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1244 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1245 | adjust_modrm_seg(ctxt, base_reg); |
| 1246 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1247 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1248 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1249 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1250 | break; |
| 1251 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1252 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1253 | break; |
| 1254 | } |
| 1255 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1256 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1257 | if (ctxt->ad_bytes != 8) |
| 1258 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1259 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1260 | done: |
| 1261 | return rc; |
| 1262 | } |
| 1263 | |
| 1264 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1265 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1266 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1267 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1268 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1269 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1270 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1271 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1272 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1275 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1276 | break; |
| 1277 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1278 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1279 | break; |
| 1280 | } |
| 1281 | done: |
| 1282 | return rc; |
| 1283 | } |
| 1284 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1285 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1286 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1287 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1288 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1289 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1290 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1291 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1292 | if (ctxt->src.bytes == 2) |
| 1293 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1294 | else if (ctxt->src.bytes == 4) |
| 1295 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1296 | else |
| 1297 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1298 | |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 1299 | ctxt->dst.addr.mem.ea = address_mask(ctxt, |
| 1300 | ctxt->dst.addr.mem.ea + (sv >> 3)); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1301 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1302 | |
| 1303 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1304 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1307 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1308 | unsigned long addr, void *dest, unsigned size) |
| 1309 | { |
| 1310 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1311 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1312 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1313 | if (mc->pos < mc->end) |
| 1314 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1315 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1316 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1317 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1318 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1319 | &ctxt->exception); |
| 1320 | if (rc != X86EMUL_CONTINUE) |
| 1321 | return rc; |
| 1322 | |
| 1323 | mc->end += size; |
| 1324 | |
| 1325 | read_cached: |
| 1326 | memcpy(dest, mc->data + mc->pos, size); |
| 1327 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1328 | return X86EMUL_CONTINUE; |
| 1329 | } |
| 1330 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1331 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1332 | struct segmented_address addr, |
| 1333 | void *data, |
| 1334 | unsigned size) |
| 1335 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1336 | int rc; |
| 1337 | ulong linear; |
| 1338 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1339 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1340 | if (rc != X86EMUL_CONTINUE) |
| 1341 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1342 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1346 | struct segmented_address addr, |
| 1347 | const void *data, |
| 1348 | unsigned size) |
| 1349 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1350 | int rc; |
| 1351 | ulong linear; |
| 1352 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1353 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1354 | if (rc != X86EMUL_CONTINUE) |
| 1355 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1356 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1357 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1361 | struct segmented_address addr, |
| 1362 | const void *orig_data, const void *data, |
| 1363 | unsigned size) |
| 1364 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1365 | int rc; |
| 1366 | ulong linear; |
| 1367 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1368 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1369 | if (rc != X86EMUL_CONTINUE) |
| 1370 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1371 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1372 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1375 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1376 | unsigned int size, unsigned short port, |
| 1377 | void *dest) |
| 1378 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1379 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1380 | |
| 1381 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1382 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1383 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1384 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1385 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1386 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1387 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Mark Rustad | b55a814 | 2014-07-25 06:27:05 -0700 | [diff] [blame] | 1388 | n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1389 | if (n == 0) |
| 1390 | n = 1; |
| 1391 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1392 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1393 | return 0; |
| 1394 | rc->end = n * size; |
| 1395 | } |
| 1396 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1397 | if (ctxt->rep_prefix && (ctxt->d & String) && |
| 1398 | !(ctxt->eflags & EFLG_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1399 | ctxt->dst.data = rc->data + rc->pos; |
| 1400 | ctxt->dst.type = OP_MEM_STR; |
| 1401 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1402 | rc->pos = rc->end; |
| 1403 | } else { |
| 1404 | memcpy(dest, rc->data + rc->pos, size); |
| 1405 | rc->pos += size; |
| 1406 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1407 | return 1; |
| 1408 | } |
| 1409 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1410 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1411 | u16 index, struct desc_struct *desc) |
| 1412 | { |
| 1413 | struct desc_ptr dt; |
| 1414 | ulong addr; |
| 1415 | |
| 1416 | ctxt->ops->get_idt(ctxt, &dt); |
| 1417 | |
| 1418 | if (dt.size < index * 8 + 7) |
| 1419 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1420 | |
| 1421 | addr = dt.address + index * 8; |
| 1422 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1423 | &ctxt->exception); |
| 1424 | } |
| 1425 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1426 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1427 | u16 selector, struct desc_ptr *dt) |
| 1428 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1429 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1430 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1431 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1432 | if (selector & 1 << 2) { |
| 1433 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1434 | u16 sel; |
| 1435 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1436 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1437 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1438 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1439 | return; |
| 1440 | |
| 1441 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1442 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1443 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1444 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1447 | static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt, |
| 1448 | u16 selector, ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1449 | { |
| 1450 | struct desc_ptr dt; |
| 1451 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1452 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1453 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1454 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1455 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1456 | if (dt.size < index * 8 + 7) |
| 1457 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1458 | |
| 1459 | addr = dt.address + index * 8; |
Nadav Amit | edccda7 | 2014-12-25 02:52:23 +0200 | [diff] [blame] | 1460 | |
| 1461 | #ifdef CONFIG_X86_64 |
| 1462 | if (addr >> 32 != 0) { |
| 1463 | u64 efer = 0; |
| 1464 | |
| 1465 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1466 | if (!(efer & EFER_LMA)) |
| 1467 | addr &= (u32)-1; |
| 1468 | } |
| 1469 | #endif |
| 1470 | |
| 1471 | *desc_addr_p = addr; |
| 1472 | return X86EMUL_CONTINUE; |
| 1473 | } |
| 1474 | |
| 1475 | /* allowed just for 8 bytes segments */ |
| 1476 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1477 | u16 selector, struct desc_struct *desc, |
| 1478 | ulong *desc_addr_p) |
| 1479 | { |
| 1480 | int rc; |
| 1481 | |
| 1482 | rc = get_descriptor_ptr(ctxt, selector, desc_addr_p); |
| 1483 | if (rc != X86EMUL_CONTINUE) |
| 1484 | return rc; |
| 1485 | |
| 1486 | return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc), |
| 1487 | &ctxt->exception); |
| 1488 | } |
| 1489 | |
| 1490 | /* allowed just for 8 bytes segments */ |
| 1491 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1492 | u16 selector, struct desc_struct *desc) |
| 1493 | { |
| 1494 | int rc; |
| 1495 | ulong addr; |
| 1496 | |
| 1497 | rc = get_descriptor_ptr(ctxt, selector, &addr); |
| 1498 | if (rc != X86EMUL_CONTINUE) |
| 1499 | return rc; |
| 1500 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1501 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1502 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1505 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1506 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1507 | u16 selector, int seg, u8 cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1508 | enum x86_transfer_type transfer, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1509 | struct desc_struct *desc) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1510 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1511 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1512 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1513 | unsigned err_vec = GP_VECTOR; |
| 1514 | u32 err_code = 0; |
| 1515 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1516 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1517 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1518 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1519 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1520 | |
| 1521 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1522 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1523 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1524 | /* set real mode segment descriptor (keep limit etc. for |
| 1525 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1526 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1527 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1528 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1529 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1530 | /* VM86 needs a clean new segment descriptor */ |
| 1531 | set_desc_base(&seg_desc, selector << 4); |
| 1532 | set_desc_limit(&seg_desc, 0xffff); |
| 1533 | seg_desc.type = 3; |
| 1534 | seg_desc.p = 1; |
| 1535 | seg_desc.s = 1; |
| 1536 | seg_desc.dpl = 3; |
| 1537 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1540 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1541 | |
| 1542 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1543 | if ((seg == VCPU_SREG_CS |
| 1544 | || (seg == VCPU_SREG_SS |
| 1545 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1546 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1547 | && null_selector) |
| 1548 | goto exception; |
| 1549 | |
| 1550 | /* TR should be in GDT only */ |
| 1551 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1552 | goto exception; |
| 1553 | |
| 1554 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1555 | goto load; |
| 1556 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1557 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1558 | if (ret != X86EMUL_CONTINUE) |
| 1559 | return ret; |
| 1560 | |
| 1561 | err_code = selector & 0xfffc; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1562 | err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR : |
| 1563 | GP_VECTOR; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1564 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1565 | /* can't load system descriptor into segment selector */ |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1566 | if (seg <= VCPU_SREG_GS && !seg_desc.s) { |
| 1567 | if (transfer == X86_TRANSFER_CALL_JMP) |
| 1568 | return X86EMUL_UNHANDLEABLE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1569 | goto exception; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1570 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1571 | |
| 1572 | if (!seg_desc.p) { |
| 1573 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1574 | goto exception; |
| 1575 | } |
| 1576 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1577 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1578 | |
| 1579 | switch (seg) { |
| 1580 | case VCPU_SREG_SS: |
| 1581 | /* |
| 1582 | * segment is not a writable data segment or segment |
| 1583 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1584 | */ |
| 1585 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1586 | goto exception; |
| 1587 | break; |
| 1588 | case VCPU_SREG_CS: |
| 1589 | if (!(seg_desc.type & 8)) |
| 1590 | goto exception; |
| 1591 | |
| 1592 | if (seg_desc.type & 4) { |
| 1593 | /* conforming */ |
| 1594 | if (dpl > cpl) |
| 1595 | goto exception; |
| 1596 | } else { |
| 1597 | /* nonconforming */ |
| 1598 | if (rpl > cpl || dpl != cpl) |
| 1599 | goto exception; |
| 1600 | } |
Nadav Amit | 040c8dc | 2014-09-18 22:39:43 +0300 | [diff] [blame] | 1601 | /* in long-mode d/b must be clear if l is set */ |
| 1602 | if (seg_desc.d && seg_desc.l) { |
| 1603 | u64 efer = 0; |
| 1604 | |
| 1605 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1606 | if (efer & EFER_LMA) |
| 1607 | goto exception; |
| 1608 | } |
| 1609 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1610 | /* CS(RPL) <- CPL */ |
| 1611 | selector = (selector & 0xfffc) | cpl; |
| 1612 | break; |
| 1613 | case VCPU_SREG_TR: |
| 1614 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1615 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1616 | old_desc = seg_desc; |
| 1617 | seg_desc.type |= 2; /* busy */ |
| 1618 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1619 | sizeof(seg_desc), &ctxt->exception); |
| 1620 | if (ret != X86EMUL_CONTINUE) |
| 1621 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1622 | break; |
| 1623 | case VCPU_SREG_LDTR: |
| 1624 | if (seg_desc.s || seg_desc.type != 2) |
| 1625 | goto exception; |
| 1626 | break; |
| 1627 | default: /* DS, ES, FS, or GS */ |
| 1628 | /* |
| 1629 | * segment is not a data or readable code segment or |
| 1630 | * ((segment is a data or nonconforming code segment) |
| 1631 | * and (both RPL and CPL > DPL)) |
| 1632 | */ |
| 1633 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1634 | (((seg_desc.type & 0xc) != 0xc) && |
| 1635 | (rpl > dpl && cpl > dpl))) |
| 1636 | goto exception; |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | if (seg_desc.s) { |
| 1641 | /* mark segment as accessed */ |
Nadav Amit | e2cefa7 | 2014-12-25 02:52:22 +0200 | [diff] [blame] | 1642 | if (!(seg_desc.type & 1)) { |
| 1643 | seg_desc.type |= 1; |
| 1644 | ret = write_segment_descriptor(ctxt, selector, |
| 1645 | &seg_desc); |
| 1646 | if (ret != X86EMUL_CONTINUE) |
| 1647 | return ret; |
| 1648 | } |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1649 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1650 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1651 | sizeof(base3), &ctxt->exception); |
| 1652 | if (ret != X86EMUL_CONTINUE) |
| 1653 | return ret; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 1654 | if (is_noncanonical_address(get_desc_base(&seg_desc) | |
| 1655 | ((u64)base3 << 32))) |
| 1656 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1657 | } |
| 1658 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1659 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1660 | if (desc) |
| 1661 | *desc = seg_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1662 | return X86EMUL_CONTINUE; |
| 1663 | exception: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 1664 | return emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1665 | } |
| 1666 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1667 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1668 | u16 selector, int seg) |
| 1669 | { |
| 1670 | u8 cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1671 | return __load_segment_descriptor(ctxt, selector, seg, cpl, |
| 1672 | X86_TRANSFER_NONE, NULL); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1673 | } |
| 1674 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1675 | static void write_register_operand(struct operand *op) |
| 1676 | { |
| 1677 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1678 | switch (op->bytes) { |
| 1679 | case 1: |
| 1680 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1681 | break; |
| 1682 | case 2: |
| 1683 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1684 | break; |
| 1685 | case 4: |
| 1686 | *op->addr.reg = (u32)op->val; |
| 1687 | break; /* 64b: zero-extend */ |
| 1688 | case 8: |
| 1689 | *op->addr.reg = op->val; |
| 1690 | break; |
| 1691 | } |
| 1692 | } |
| 1693 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1694 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1695 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1696 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1697 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1698 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1699 | break; |
| 1700 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1701 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1702 | return segmented_cmpxchg(ctxt, |
| 1703 | op->addr.mem, |
| 1704 | &op->orig_val, |
| 1705 | &op->val, |
| 1706 | op->bytes); |
| 1707 | else |
| 1708 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1709 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1710 | &op->val, |
| 1711 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1712 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1713 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1714 | return segmented_write(ctxt, |
| 1715 | op->addr.mem, |
| 1716 | op->data, |
| 1717 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1718 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1719 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1720 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1721 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1722 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1723 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1724 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1725 | case OP_NONE: |
| 1726 | /* no writeback */ |
| 1727 | break; |
| 1728 | default: |
| 1729 | break; |
| 1730 | } |
| 1731 | return X86EMUL_CONTINUE; |
| 1732 | } |
| 1733 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1734 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1735 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1736 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1737 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1738 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1739 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1740 | addr.seg = VCPU_SREG_SS; |
| 1741 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1742 | return segmented_write(ctxt, addr, data, bytes); |
| 1743 | } |
| 1744 | |
| 1745 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1746 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1747 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1748 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1749 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1752 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1753 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1754 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1755 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1756 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1757 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1758 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1759 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1760 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1761 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1762 | return rc; |
| 1763 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1764 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1765 | return rc; |
| 1766 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1767 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1768 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1769 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1770 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1771 | } |
| 1772 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1773 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1774 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1775 | { |
| 1776 | int rc; |
| 1777 | unsigned long val, change_mask; |
| 1778 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1779 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1780 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1781 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1782 | if (rc != X86EMUL_CONTINUE) |
| 1783 | return rc; |
| 1784 | |
| 1785 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
Nadav Amit | 163b135 | 2014-07-21 14:37:28 +0300 | [diff] [blame] | 1786 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1787 | |
| 1788 | switch(ctxt->mode) { |
| 1789 | case X86EMUL_MODE_PROT64: |
| 1790 | case X86EMUL_MODE_PROT32: |
| 1791 | case X86EMUL_MODE_PROT16: |
| 1792 | if (cpl == 0) |
| 1793 | change_mask |= EFLG_IOPL; |
| 1794 | if (cpl <= iopl) |
| 1795 | change_mask |= EFLG_IF; |
| 1796 | break; |
| 1797 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1798 | if (iopl < 3) |
| 1799 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1800 | change_mask |= EFLG_IF; |
| 1801 | break; |
| 1802 | default: /* real mode */ |
| 1803 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1804 | break; |
| 1805 | } |
| 1806 | |
| 1807 | *(unsigned long *)dest = |
| 1808 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1809 | |
| 1810 | return rc; |
| 1811 | } |
| 1812 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1813 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1814 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1815 | ctxt->dst.type = OP_REG; |
| 1816 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1817 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1818 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1819 | } |
| 1820 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1821 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1822 | { |
| 1823 | int rc; |
| 1824 | unsigned frame_size = ctxt->src.val; |
| 1825 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1826 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1827 | |
| 1828 | if (nesting_level) |
| 1829 | return X86EMUL_UNHANDLEABLE; |
| 1830 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1831 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1832 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1833 | if (rc != X86EMUL_CONTINUE) |
| 1834 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1835 | 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] | 1836 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1837 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1838 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1839 | stack_mask(ctxt)); |
| 1840 | return X86EMUL_CONTINUE; |
| 1841 | } |
| 1842 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1843 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1844 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1845 | 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] | 1846 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1847 | 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] | 1848 | } |
| 1849 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1850 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1851 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1852 | int seg = ctxt->src2.val; |
| 1853 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1854 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Nadav Amit | 0fcc207 | 2014-11-02 11:54:51 +0200 | [diff] [blame] | 1855 | if (ctxt->op_bytes == 4) { |
| 1856 | rsp_increment(ctxt, -2); |
| 1857 | ctxt->op_bytes = 2; |
| 1858 | } |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1859 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1860 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1861 | } |
| 1862 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1863 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1864 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1865 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1866 | unsigned long selector; |
| 1867 | int rc; |
| 1868 | |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1869 | rc = emulate_pop(ctxt, &selector, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1870 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1871 | return rc; |
| 1872 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1873 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1874 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1875 | if (ctxt->op_bytes > 2) |
| 1876 | rsp_increment(ctxt, ctxt->op_bytes - 2); |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1877 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1878 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1879 | return rc; |
| 1880 | } |
| 1881 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1882 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1883 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1884 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1885 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1886 | int reg = VCPU_REGS_RAX; |
| 1887 | |
| 1888 | while (reg <= VCPU_REGS_RDI) { |
| 1889 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1890 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1891 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1892 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1893 | if (rc != X86EMUL_CONTINUE) |
| 1894 | return rc; |
| 1895 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1896 | ++reg; |
| 1897 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1898 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1899 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1900 | } |
| 1901 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1902 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1903 | { |
Nadav Amit | bc397a6 | 2014-12-10 11:19:03 +0200 | [diff] [blame] | 1904 | ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1905 | return em_push(ctxt); |
| 1906 | } |
| 1907 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1908 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1909 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1910 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1911 | int reg = VCPU_REGS_RDI; |
| 1912 | |
| 1913 | while (reg >= VCPU_REGS_RAX) { |
| 1914 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1915 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1916 | --reg; |
| 1917 | } |
| 1918 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1919 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1920 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1921 | break; |
| 1922 | --reg; |
| 1923 | } |
| 1924 | return rc; |
| 1925 | } |
| 1926 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1927 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1928 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1929 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1930 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1931 | struct desc_ptr dt; |
| 1932 | gva_t cs_addr; |
| 1933 | gva_t eip_addr; |
| 1934 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1935 | |
| 1936 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1937 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1938 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1939 | if (rc != X86EMUL_CONTINUE) |
| 1940 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1941 | |
| 1942 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1943 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1944 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1945 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1946 | if (rc != X86EMUL_CONTINUE) |
| 1947 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1948 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1949 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1950 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1951 | if (rc != X86EMUL_CONTINUE) |
| 1952 | return rc; |
| 1953 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1954 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1955 | |
| 1956 | eip_addr = dt.address + (irq << 2); |
| 1957 | cs_addr = dt.address + (irq << 2) + 2; |
| 1958 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1959 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1960 | if (rc != X86EMUL_CONTINUE) |
| 1961 | return rc; |
| 1962 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1963 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1964 | if (rc != X86EMUL_CONTINUE) |
| 1965 | return rc; |
| 1966 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1967 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1968 | if (rc != X86EMUL_CONTINUE) |
| 1969 | return rc; |
| 1970 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1971 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1972 | |
| 1973 | return rc; |
| 1974 | } |
| 1975 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1976 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1977 | { |
| 1978 | int rc; |
| 1979 | |
| 1980 | invalidate_registers(ctxt); |
| 1981 | rc = __emulate_int_real(ctxt, irq); |
| 1982 | if (rc == X86EMUL_CONTINUE) |
| 1983 | writeback_registers(ctxt); |
| 1984 | return rc; |
| 1985 | } |
| 1986 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1987 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1988 | { |
| 1989 | switch(ctxt->mode) { |
| 1990 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1991 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1992 | case X86EMUL_MODE_VM86: |
| 1993 | case X86EMUL_MODE_PROT16: |
| 1994 | case X86EMUL_MODE_PROT32: |
| 1995 | case X86EMUL_MODE_PROT64: |
| 1996 | default: |
| 1997 | /* Protected mode interrupts unimplemented yet */ |
| 1998 | return X86EMUL_UNHANDLEABLE; |
| 1999 | } |
| 2000 | } |
| 2001 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2002 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2003 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2004 | int rc = X86EMUL_CONTINUE; |
| 2005 | unsigned long temp_eip = 0; |
| 2006 | unsigned long temp_eflags = 0; |
| 2007 | unsigned long cs = 0; |
| 2008 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 2009 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 2010 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 2011 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 2012 | |
| 2013 | /* TODO: Add stack limit check */ |
| 2014 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2015 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2016 | |
| 2017 | if (rc != X86EMUL_CONTINUE) |
| 2018 | return rc; |
| 2019 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2020 | if (temp_eip & ~0xffff) |
| 2021 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2022 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2023 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2024 | |
| 2025 | if (rc != X86EMUL_CONTINUE) |
| 2026 | return rc; |
| 2027 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2028 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2029 | |
| 2030 | if (rc != X86EMUL_CONTINUE) |
| 2031 | return rc; |
| 2032 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2033 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2034 | |
| 2035 | if (rc != X86EMUL_CONTINUE) |
| 2036 | return rc; |
| 2037 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2038 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2039 | |
| 2040 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2041 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2042 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2043 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2044 | ctxt->eflags &= ~0xffff; |
| 2045 | ctxt->eflags |= temp_eflags; |
| 2046 | } |
| 2047 | |
| 2048 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 2049 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
Nadav Amit | 801806d | 2015-01-26 09:32:23 +0200 | [diff] [blame^] | 2050 | ctxt->ops->set_nmi_mask(ctxt, false); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2051 | |
| 2052 | return rc; |
| 2053 | } |
| 2054 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2055 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2056 | { |
| 2057 | switch(ctxt->mode) { |
| 2058 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2059 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2060 | case X86EMUL_MODE_VM86: |
| 2061 | case X86EMUL_MODE_PROT16: |
| 2062 | case X86EMUL_MODE_PROT32: |
| 2063 | case X86EMUL_MODE_PROT64: |
| 2064 | default: |
| 2065 | /* iret from protected mode unimplemented yet */ |
| 2066 | return X86EMUL_UNHANDLEABLE; |
| 2067 | } |
| 2068 | } |
| 2069 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2070 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2071 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2072 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2073 | unsigned short sel, old_sel; |
| 2074 | struct desc_struct old_desc, new_desc; |
| 2075 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 2076 | u8 cpl = ctxt->ops->cpl(ctxt); |
| 2077 | |
| 2078 | /* Assignment of RIP may only fail in 64-bit mode */ |
| 2079 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2080 | ops->get_segment(ctxt, &old_sel, &old_desc, NULL, |
| 2081 | VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2082 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2083 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2084 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2085 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 2086 | X86_TRANSFER_CALL_JMP, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2087 | &new_desc); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2088 | if (rc != X86EMUL_CONTINUE) |
| 2089 | return rc; |
| 2090 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2091 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2092 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2093 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2094 | /* assigning eip failed; restore the old cs */ |
| 2095 | ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS); |
| 2096 | return rc; |
| 2097 | } |
| 2098 | return rc; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2099 | } |
| 2100 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2101 | static int em_jmp_abs(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2102 | { |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2103 | return assign_eip_near(ctxt, ctxt->src.val); |
| 2104 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2105 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2106 | static int em_call_near_abs(struct x86_emulate_ctxt *ctxt) |
| 2107 | { |
| 2108 | int rc; |
| 2109 | long int old_eip; |
| 2110 | |
| 2111 | old_eip = ctxt->_eip; |
| 2112 | rc = assign_eip_near(ctxt, ctxt->src.val); |
| 2113 | if (rc != X86EMUL_CONTINUE) |
| 2114 | return rc; |
| 2115 | ctxt->src.val = old_eip; |
| 2116 | rc = em_push(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2117 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2120 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2121 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2122 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2123 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2124 | if (ctxt->dst.bytes == 16) |
| 2125 | return X86EMUL_UNHANDLEABLE; |
| 2126 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2127 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2128 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2129 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2130 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2131 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2132 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2133 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2134 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2135 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2136 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2137 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2138 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2141 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2142 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2143 | int rc; |
| 2144 | unsigned long eip; |
| 2145 | |
| 2146 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 2147 | if (rc != X86EMUL_CONTINUE) |
| 2148 | return rc; |
| 2149 | |
| 2150 | return assign_eip_near(ctxt, eip); |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2151 | } |
| 2152 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2153 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2154 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2155 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2156 | unsigned long eip, cs; |
| 2157 | u16 old_cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2158 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2159 | struct desc_struct old_desc, new_desc; |
| 2160 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2161 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2162 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2163 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, |
| 2164 | VCPU_SREG_CS); |
| 2165 | |
| 2166 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2167 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2168 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2169 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2170 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2171 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2172 | /* Outer-privilege level return is not implemented */ |
| 2173 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2174 | return X86EMUL_UNHANDLEABLE; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2175 | rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, |
| 2176 | X86_TRANSFER_RET, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2177 | &new_desc); |
| 2178 | if (rc != X86EMUL_CONTINUE) |
| 2179 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2180 | rc = assign_eip_far(ctxt, eip, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2181 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2182 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2183 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 2184 | } |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2185 | return rc; |
| 2186 | } |
| 2187 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2188 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2189 | { |
| 2190 | int rc; |
| 2191 | |
| 2192 | rc = em_ret_far(ctxt); |
| 2193 | if (rc != X86EMUL_CONTINUE) |
| 2194 | return rc; |
| 2195 | rsp_increment(ctxt, ctxt->src.val); |
| 2196 | return X86EMUL_CONTINUE; |
| 2197 | } |
| 2198 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2199 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2200 | { |
| 2201 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2202 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2203 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2204 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2205 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2206 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2207 | |
| 2208 | if (ctxt->eflags & EFLG_ZF) { |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2209 | /* Success: write back to memory; no update of EAX */ |
| 2210 | ctxt->src.type = OP_NONE; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2211 | ctxt->dst.val = ctxt->src.orig_val; |
| 2212 | } else { |
| 2213 | /* Failure: write the value we saw to EAX. */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 2214 | ctxt->src.type = OP_REG; |
| 2215 | ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 2216 | ctxt->src.val = ctxt->dst.orig_val; |
| 2217 | /* Create write-cycle to dest by writing the same value */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2218 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2219 | } |
| 2220 | return X86EMUL_CONTINUE; |
| 2221 | } |
| 2222 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2223 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2224 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2225 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2226 | unsigned short sel; |
| 2227 | int rc; |
| 2228 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2229 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2230 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2231 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2232 | if (rc != X86EMUL_CONTINUE) |
| 2233 | return rc; |
| 2234 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2235 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2236 | return rc; |
| 2237 | } |
| 2238 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2239 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2240 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2241 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2242 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2243 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2244 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2245 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2246 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2247 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2248 | cs->s = 1; |
| 2249 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2250 | cs->p = 1; |
| 2251 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2252 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2253 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2254 | set_desc_base(ss, 0); /* flat segment */ |
| 2255 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2256 | ss->g = 1; /* 4kb granularity */ |
| 2257 | ss->s = 1; |
| 2258 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2259 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2260 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2261 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2262 | ss->l = 0; |
| 2263 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2264 | } |
| 2265 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2266 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2267 | { |
| 2268 | u32 eax, ebx, ecx, edx; |
| 2269 | |
| 2270 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2271 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2272 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2273 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2274 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2275 | } |
| 2276 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2277 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2278 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2279 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2280 | u32 eax, ebx, ecx, edx; |
| 2281 | |
| 2282 | /* |
| 2283 | * syscall should always be enabled in longmode - so only become |
| 2284 | * vendor specific (cpuid) if other modes are active... |
| 2285 | */ |
| 2286 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2287 | return true; |
| 2288 | |
| 2289 | eax = 0x00000000; |
| 2290 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2291 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2292 | /* |
| 2293 | * Intel ("GenuineIntel") |
| 2294 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2295 | * longmode. Also an 64bit guest with a |
| 2296 | * 32bit compat-app running will #UD !! While this |
| 2297 | * behaviour can be fixed (by emulating) into AMD |
| 2298 | * response - CPUs of AMD can't behave like Intel. |
| 2299 | */ |
| 2300 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2301 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2302 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2303 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2304 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2305 | /* AMD ("AuthenticAMD") */ |
| 2306 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2307 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2308 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2309 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2310 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2311 | /* AMD ("AMDisbetter!") */ |
| 2312 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2313 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2314 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2315 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2316 | |
| 2317 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2318 | return false; |
| 2319 | } |
| 2320 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2321 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2322 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2323 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2324 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2325 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2326 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2327 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2328 | |
| 2329 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2330 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2331 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2332 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2333 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2334 | if (!(em_syscall_is_enabled(ctxt))) |
| 2335 | return emulate_ud(ctxt); |
| 2336 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2337 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2338 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2339 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2340 | if (!(efer & EFER_SCE)) |
| 2341 | return emulate_ud(ctxt); |
| 2342 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2343 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2344 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2345 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2346 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2347 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2348 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2349 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2350 | cs.l = 1; |
| 2351 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2352 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2353 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2354 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2355 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2356 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2357 | #ifdef CONFIG_X86_64 |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2358 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2359 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2360 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2361 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2362 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2363 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2364 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2365 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2366 | ctxt->eflags &= ~msr_data; |
Nadav Amit | 807c142 | 2014-11-02 11:54:49 +0200 | [diff] [blame] | 2367 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2368 | #endif |
| 2369 | } else { |
| 2370 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2371 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2372 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2373 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2374 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2375 | } |
| 2376 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2377 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2380 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2381 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2382 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2383 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2384 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2385 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2386 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2387 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2388 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2389 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2390 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2391 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2392 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2393 | /* |
| 2394 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2395 | * mode). |
| 2396 | */ |
| 2397 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2398 | && !vendor_intel(ctxt)) |
| 2399 | return emulate_ud(ctxt); |
| 2400 | |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2401 | /* sysenter/sysexit have not been tested in 64bit mode. */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2402 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2403 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2404 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2405 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2406 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2407 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2408 | switch (ctxt->mode) { |
| 2409 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2410 | if ((msr_data & 0xfffc) == 0x0) |
| 2411 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2412 | break; |
| 2413 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2414 | if (msr_data == 0x0) |
| 2415 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2416 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2417 | default: |
| 2418 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2421 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2422 | cs_sel = (u16)msr_data; |
| 2423 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2424 | ss_sel = cs_sel + 8; |
| 2425 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2426 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2427 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2428 | cs.l = 1; |
| 2429 | } |
| 2430 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2431 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2432 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2433 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2434 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2435 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2436 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2437 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2438 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2439 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2440 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2441 | } |
| 2442 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2443 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2444 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2445 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2446 | struct desc_struct cs, ss; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2447 | u64 msr_data, rcx, rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2448 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2449 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2450 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2451 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2452 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2453 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2454 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2455 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2456 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2457 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2458 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2459 | usermode = X86EMUL_MODE_PROT64; |
| 2460 | else |
| 2461 | usermode = X86EMUL_MODE_PROT32; |
| 2462 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2463 | rcx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2464 | rdx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2465 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2466 | cs.dpl = 3; |
| 2467 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2468 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2469 | switch (usermode) { |
| 2470 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2471 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2472 | if ((msr_data & 0xfffc) == 0x0) |
| 2473 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2474 | ss_sel = (u16)(msr_data + 24); |
Nadav Amit | bf0b682 | 2014-09-18 22:39:45 +0300 | [diff] [blame] | 2475 | rcx = (u32)rcx; |
| 2476 | rdx = (u32)rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2477 | break; |
| 2478 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2479 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2480 | if (msr_data == 0x0) |
| 2481 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2482 | ss_sel = cs_sel + 8; |
| 2483 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2484 | cs.l = 1; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2485 | if (is_noncanonical_address(rcx) || |
| 2486 | is_noncanonical_address(rdx)) |
| 2487 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2488 | break; |
| 2489 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2490 | cs_sel |= SELECTOR_RPL_MASK; |
| 2491 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2492 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2493 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2494 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2495 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2496 | ctxt->_eip = rdx; |
| 2497 | *reg_write(ctxt, VCPU_REGS_RSP) = rcx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2498 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2499 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2500 | } |
| 2501 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2502 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2503 | { |
| 2504 | int iopl; |
| 2505 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2506 | return false; |
| 2507 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2508 | return true; |
| 2509 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2510 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2514 | u16 port, u16 len) |
| 2515 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2516 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2517 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2518 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2519 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2520 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2521 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2522 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2523 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2524 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2525 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2526 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2527 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2528 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2529 | base = get_desc_base(&tr_seg); |
| 2530 | #ifdef CONFIG_X86_64 |
| 2531 | base |= ((u64)base3) << 32; |
| 2532 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2533 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2534 | if (r != X86EMUL_CONTINUE) |
| 2535 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2536 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2537 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2538 | 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] | 2539 | if (r != X86EMUL_CONTINUE) |
| 2540 | return false; |
| 2541 | if ((perm >> bit_idx) & mask) |
| 2542 | return false; |
| 2543 | return true; |
| 2544 | } |
| 2545 | |
| 2546 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2547 | u16 port, u16 len) |
| 2548 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2549 | if (ctxt->perm_ok) |
| 2550 | return true; |
| 2551 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2552 | if (emulator_bad_iopl(ctxt)) |
| 2553 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2554 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2555 | |
| 2556 | ctxt->perm_ok = true; |
| 2557 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2558 | return true; |
| 2559 | } |
| 2560 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2561 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2562 | struct tss_segment_16 *tss) |
| 2563 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2564 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2565 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2566 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2567 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2568 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2569 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2570 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2571 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2572 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2573 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2574 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2575 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2576 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2577 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2578 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2579 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2583 | struct tss_segment_16 *tss) |
| 2584 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2585 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2586 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2587 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2588 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2589 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2590 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2591 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2592 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2593 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2594 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2595 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2596 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2597 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | |
| 2599 | /* |
| 2600 | * SDM says that segment selectors are loaded before segment |
| 2601 | * descriptors |
| 2602 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2603 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2604 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2605 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2606 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2607 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2608 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2609 | cpl = tss->cs & 3; |
| 2610 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2611 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2612 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2613 | * it is handled in a context of new task |
| 2614 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2615 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2616 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2617 | if (ret != X86EMUL_CONTINUE) |
| 2618 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2619 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2620 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2621 | if (ret != X86EMUL_CONTINUE) |
| 2622 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2623 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2624 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2625 | if (ret != X86EMUL_CONTINUE) |
| 2626 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2627 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2628 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2629 | if (ret != X86EMUL_CONTINUE) |
| 2630 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2631 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2632 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2633 | if (ret != X86EMUL_CONTINUE) |
| 2634 | return ret; |
| 2635 | |
| 2636 | return X86EMUL_CONTINUE; |
| 2637 | } |
| 2638 | |
| 2639 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | u16 tss_selector, u16 old_tss_sel, |
| 2641 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2642 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2643 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2644 | struct tss_segment_16 tss_seg; |
| 2645 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2646 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2648 | 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] | 2649 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2650 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2651 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2652 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2653 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2654 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2655 | 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] | 2656 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2657 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2658 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2659 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2660 | 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] | 2661 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2662 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2663 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2664 | |
| 2665 | if (old_tss_sel != 0xffff) { |
| 2666 | tss_seg.prev_task_link = old_tss_sel; |
| 2667 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2668 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2669 | &tss_seg.prev_task_link, |
| 2670 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2671 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2672 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2673 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2676 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2680 | struct tss_segment_32 *tss) |
| 2681 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2682 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2683 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2684 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2685 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2686 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2687 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2688 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2689 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2690 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2691 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2692 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2693 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2694 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2695 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2696 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2697 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2698 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2699 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2703 | struct tss_segment_32 *tss) |
| 2704 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2705 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2706 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2707 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2708 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2709 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2710 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2711 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2714 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2715 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2716 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2717 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2718 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2719 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2720 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2721 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2722 | |
| 2723 | /* |
| 2724 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2725 | * descriptors. This is important because CPL checks will |
| 2726 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2727 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2728 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2729 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2730 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2731 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2732 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2733 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2734 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2735 | |
| 2736 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2737 | * If we're switching between Protected Mode and VM86, we need to make |
| 2738 | * sure to update the mode before loading the segment descriptors so |
| 2739 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2740 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2741 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2742 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2743 | cpl = 3; |
| 2744 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2745 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2746 | cpl = tss->cs & 3; |
| 2747 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2748 | |
| 2749 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2750 | * Now load segment descriptors. If fault happenes at this stage |
| 2751 | * it is handled in a context of new task |
| 2752 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2753 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2754 | cpl, X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2755 | if (ret != X86EMUL_CONTINUE) |
| 2756 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2757 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2758 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2759 | if (ret != X86EMUL_CONTINUE) |
| 2760 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2761 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2762 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2763 | if (ret != X86EMUL_CONTINUE) |
| 2764 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2765 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2766 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2767 | if (ret != X86EMUL_CONTINUE) |
| 2768 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2769 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2770 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2771 | if (ret != X86EMUL_CONTINUE) |
| 2772 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2773 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2774 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2775 | if (ret != X86EMUL_CONTINUE) |
| 2776 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2777 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2778 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2779 | if (ret != X86EMUL_CONTINUE) |
| 2780 | return ret; |
| 2781 | |
| 2782 | return X86EMUL_CONTINUE; |
| 2783 | } |
| 2784 | |
| 2785 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2786 | u16 tss_selector, u16 old_tss_sel, |
| 2787 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2788 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2789 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2790 | struct tss_segment_32 tss_seg; |
| 2791 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2792 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2793 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2794 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2795 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2796 | 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] | 2797 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2798 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2799 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2800 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2801 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2802 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2803 | /* Only GP registers and segment selectors are saved */ |
| 2804 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2805 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2806 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2807 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2808 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2809 | ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2810 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2811 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2812 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2813 | |
| 2814 | if (old_tss_sel != 0xffff) { |
| 2815 | tss_seg.prev_task_link = old_tss_sel; |
| 2816 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2817 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2818 | &tss_seg.prev_task_link, |
| 2819 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2820 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2821 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2822 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2825 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2829 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2830 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2831 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2832 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2833 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2834 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2835 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2836 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2837 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2838 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2839 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2840 | |
| 2841 | /* FIXME: old_tss_base == ~0 ? */ |
| 2842 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2843 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2844 | if (ret != X86EMUL_CONTINUE) |
| 2845 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2846 | 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] | 2847 | if (ret != X86EMUL_CONTINUE) |
| 2848 | return ret; |
| 2849 | |
| 2850 | /* FIXME: check that next_tss_desc is tss */ |
| 2851 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2852 | /* |
| 2853 | * Check privileges. The three cases are task switch caused by... |
| 2854 | * |
| 2855 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2856 | * 2. Exception/IRQ/iret: No check is performed |
Nadav Amit | 2c2ca2d | 2014-11-02 11:54:57 +0200 | [diff] [blame] | 2857 | * 3. jmp/call to TSS/task-gate: No check is performed since the |
| 2858 | * hardware checks it before exiting. |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2859 | */ |
| 2860 | if (reason == TASK_SWITCH_GATE) { |
| 2861 | if (idt_index != -1) { |
| 2862 | /* Software interrupts */ |
| 2863 | struct desc_struct task_gate_desc; |
| 2864 | int dpl; |
| 2865 | |
| 2866 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2867 | &task_gate_desc); |
| 2868 | if (ret != X86EMUL_CONTINUE) |
| 2869 | return ret; |
| 2870 | |
| 2871 | dpl = task_gate_desc.dpl; |
| 2872 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2873 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2874 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2875 | } |
| 2876 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2877 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2878 | if (!next_tss_desc.p || |
| 2879 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2880 | desc_limit < 0x2b)) { |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 2881 | return emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2885 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2886 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | if (reason == TASK_SWITCH_IRET) |
| 2890 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2891 | |
| 2892 | /* 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] | 2893 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2894 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2895 | old_tss_sel = 0xffff; |
| 2896 | |
| 2897 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2898 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2899 | old_tss_base, &next_tss_desc); |
| 2900 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2901 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2902 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2903 | if (ret != X86EMUL_CONTINUE) |
| 2904 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2905 | |
| 2906 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2907 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2908 | |
| 2909 | if (reason != TASK_SWITCH_IRET) { |
| 2910 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2911 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2912 | } |
| 2913 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2914 | 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] | 2915 | 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] | 2916 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2917 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2918 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2919 | ctxt->lock_prefix = 0; |
| 2920 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2921 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2924 | return ret; |
| 2925 | } |
| 2926 | |
| 2927 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2928 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2929 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2930 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2931 | int rc; |
| 2932 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2933 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2934 | ctxt->_eip = ctxt->eip; |
| 2935 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2936 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2937 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2938 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2939 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2940 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2941 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2942 | writeback_registers(ctxt); |
| 2943 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2944 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2945 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2948 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2949 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2950 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2951 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2952 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 2953 | register_address_increment(ctxt, reg, df * op->bytes); |
| 2954 | op->addr.mem.ea = register_address(ctxt, reg); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2957 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2958 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2959 | u8 al, old_al; |
| 2960 | bool af, cf, old_cf; |
| 2961 | |
| 2962 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2963 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2964 | |
| 2965 | old_al = al; |
| 2966 | old_cf = cf; |
| 2967 | cf = false; |
| 2968 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2969 | if ((al & 0x0f) > 9 || af) { |
| 2970 | al -= 6; |
| 2971 | cf = old_cf | (al >= 250); |
| 2972 | af = true; |
| 2973 | } else { |
| 2974 | af = false; |
| 2975 | } |
| 2976 | if (old_al > 0x99 || old_cf) { |
| 2977 | al -= 0x60; |
| 2978 | cf = true; |
| 2979 | } |
| 2980 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2981 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2982 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2983 | ctxt->src.type = OP_IMM; |
| 2984 | ctxt->src.val = 0; |
| 2985 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2986 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2987 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2988 | if (cf) |
| 2989 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2990 | if (af) |
| 2991 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2992 | return X86EMUL_CONTINUE; |
| 2993 | } |
| 2994 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2995 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2996 | { |
| 2997 | u8 al, ah; |
| 2998 | |
| 2999 | if (ctxt->src.val == 0) |
| 3000 | return emulate_de(ctxt); |
| 3001 | |
| 3002 | al = ctxt->dst.val & 0xff; |
| 3003 | ah = al / ctxt->src.val; |
| 3004 | al %= ctxt->src.val; |
| 3005 | |
| 3006 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 3007 | |
| 3008 | /* Set PF, ZF, SF */ |
| 3009 | ctxt->src.type = OP_IMM; |
| 3010 | ctxt->src.val = 0; |
| 3011 | ctxt->src.bytes = 1; |
| 3012 | fastop(ctxt, em_or); |
| 3013 | |
| 3014 | return X86EMUL_CONTINUE; |
| 3015 | } |
| 3016 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3017 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 3018 | { |
| 3019 | u8 al = ctxt->dst.val & 0xff; |
| 3020 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 3021 | |
| 3022 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 3023 | |
| 3024 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 3025 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 3026 | /* Set PF, ZF, SF */ |
| 3027 | ctxt->src.type = OP_IMM; |
| 3028 | ctxt->src.val = 0; |
| 3029 | ctxt->src.bytes = 1; |
| 3030 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3031 | |
| 3032 | return X86EMUL_CONTINUE; |
| 3033 | } |
| 3034 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3035 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3036 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3037 | int rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3038 | long rel = ctxt->src.val; |
| 3039 | |
| 3040 | ctxt->src.val = (unsigned long)ctxt->_eip; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3041 | rc = jmp_rel(ctxt, rel); |
| 3042 | if (rc != X86EMUL_CONTINUE) |
| 3043 | return rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3044 | return em_push(ctxt); |
| 3045 | } |
| 3046 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3047 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3048 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3049 | u16 sel, old_cs; |
| 3050 | ulong old_eip; |
| 3051 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3052 | struct desc_struct old_desc, new_desc; |
| 3053 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 3054 | int cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3055 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3056 | old_eip = ctxt->_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3057 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3058 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3059 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3060 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 3061 | X86_TRANSFER_CALL_JMP, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3062 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | 80976db | 2014-12-25 02:52:20 +0200 | [diff] [blame] | 3063 | return rc; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3064 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 3065 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3066 | if (rc != X86EMUL_CONTINUE) |
| 3067 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3068 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3069 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3070 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3071 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3072 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3073 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3074 | ctxt->src.val = old_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3075 | rc = em_push(ctxt); |
| 3076 | /* If we failed, we tainted the memory, but the very least we should |
| 3077 | restore cs */ |
| 3078 | if (rc != X86EMUL_CONTINUE) |
| 3079 | goto fail; |
| 3080 | return rc; |
| 3081 | fail: |
| 3082 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 3083 | return rc; |
| 3084 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3085 | } |
| 3086 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3087 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3088 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3089 | int rc; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3090 | unsigned long eip; |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3091 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3092 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 3093 | if (rc != X86EMUL_CONTINUE) |
| 3094 | return rc; |
| 3095 | rc = assign_eip_near(ctxt, eip); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3096 | if (rc != X86EMUL_CONTINUE) |
| 3097 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3098 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3099 | return X86EMUL_CONTINUE; |
| 3100 | } |
| 3101 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3102 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3103 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3104 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3105 | ctxt->src.val = ctxt->dst.val; |
| 3106 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3107 | |
| 3108 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3109 | ctxt->dst.val = ctxt->src.orig_val; |
| 3110 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3111 | return X86EMUL_CONTINUE; |
| 3112 | } |
| 3113 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3114 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3115 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3116 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3117 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3118 | } |
| 3119 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3120 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3121 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3122 | ctxt->dst.type = OP_REG; |
| 3123 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3124 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3125 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3126 | |
| 3127 | return X86EMUL_CONTINUE; |
| 3128 | } |
| 3129 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3130 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3131 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3132 | u64 tsc = 0; |
| 3133 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3134 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3135 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3136 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3137 | return X86EMUL_CONTINUE; |
| 3138 | } |
| 3139 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3140 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3141 | { |
| 3142 | u64 pmc; |
| 3143 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3144 | 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] | 3145 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3146 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3147 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3148 | return X86EMUL_CONTINUE; |
| 3149 | } |
| 3150 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3151 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3152 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 3153 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3154 | return X86EMUL_CONTINUE; |
| 3155 | } |
| 3156 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3157 | #define FFL(x) bit(X86_FEATURE_##x) |
| 3158 | |
| 3159 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 3160 | { |
| 3161 | u32 ebx, ecx, edx, eax = 1; |
| 3162 | u16 tmp; |
| 3163 | |
| 3164 | /* |
| 3165 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 3166 | */ |
| 3167 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3168 | if (!(ecx & FFL(MOVBE))) |
| 3169 | return emulate_ud(ctxt); |
| 3170 | |
| 3171 | switch (ctxt->op_bytes) { |
| 3172 | case 2: |
| 3173 | /* |
| 3174 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3175 | * the upper word of the destination register remains unchanged |
| 3176 | * ..." |
| 3177 | * |
| 3178 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3179 | * rules so we have to do the operation almost per hand. |
| 3180 | */ |
| 3181 | tmp = (u16)ctxt->src.val; |
| 3182 | ctxt->dst.val &= ~0xffffUL; |
| 3183 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3184 | break; |
| 3185 | case 4: |
| 3186 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3187 | break; |
| 3188 | case 8: |
| 3189 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3190 | break; |
| 3191 | default: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3192 | BUG(); |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3193 | } |
| 3194 | return X86EMUL_CONTINUE; |
| 3195 | } |
| 3196 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3197 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3198 | { |
| 3199 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3200 | return emulate_gp(ctxt, 0); |
| 3201 | |
| 3202 | /* Disable writeback. */ |
| 3203 | ctxt->dst.type = OP_NONE; |
| 3204 | return X86EMUL_CONTINUE; |
| 3205 | } |
| 3206 | |
| 3207 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3208 | { |
| 3209 | unsigned long val; |
| 3210 | |
| 3211 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3212 | val = ctxt->src.val & ~0ULL; |
| 3213 | else |
| 3214 | val = ctxt->src.val & ~0U; |
| 3215 | |
| 3216 | /* #UD condition is already handled. */ |
| 3217 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3218 | return emulate_gp(ctxt, 0); |
| 3219 | |
| 3220 | /* Disable writeback. */ |
| 3221 | ctxt->dst.type = OP_NONE; |
| 3222 | return X86EMUL_CONTINUE; |
| 3223 | } |
| 3224 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3225 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3226 | { |
| 3227 | u64 msr_data; |
| 3228 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3229 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3230 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3231 | 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] | 3232 | return emulate_gp(ctxt, 0); |
| 3233 | |
| 3234 | return X86EMUL_CONTINUE; |
| 3235 | } |
| 3236 | |
| 3237 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3238 | { |
| 3239 | u64 msr_data; |
| 3240 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3241 | 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] | 3242 | return emulate_gp(ctxt, 0); |
| 3243 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3244 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3245 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3246 | return X86EMUL_CONTINUE; |
| 3247 | } |
| 3248 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3249 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3250 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3251 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3252 | return emulate_ud(ctxt); |
| 3253 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3254 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Nadav Amit | b5bbf10 | 2014-11-02 11:54:46 +0200 | [diff] [blame] | 3255 | if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM) |
| 3256 | ctxt->dst.bytes = 2; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3257 | return X86EMUL_CONTINUE; |
| 3258 | } |
| 3259 | |
| 3260 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3261 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3262 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3263 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3264 | 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] | 3265 | return emulate_ud(ctxt); |
| 3266 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3267 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3268 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3269 | |
| 3270 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3271 | ctxt->dst.type = OP_NONE; |
| 3272 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3273 | } |
| 3274 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3275 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3276 | { |
| 3277 | u16 sel = ctxt->src.val; |
| 3278 | |
| 3279 | /* Disable writeback. */ |
| 3280 | ctxt->dst.type = OP_NONE; |
| 3281 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3282 | } |
| 3283 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3284 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3285 | { |
| 3286 | u16 sel = ctxt->src.val; |
| 3287 | |
| 3288 | /* Disable writeback. */ |
| 3289 | ctxt->dst.type = OP_NONE; |
| 3290 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3291 | } |
| 3292 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3293 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3294 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3295 | int rc; |
| 3296 | ulong linear; |
| 3297 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3298 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3299 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3300 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3301 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3302 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3303 | return X86EMUL_CONTINUE; |
| 3304 | } |
| 3305 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3306 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3307 | { |
| 3308 | ulong cr0; |
| 3309 | |
| 3310 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3311 | cr0 &= ~X86_CR0_TS; |
| 3312 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3313 | return X86EMUL_CONTINUE; |
| 3314 | } |
| 3315 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3316 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3317 | { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3318 | int rc = ctxt->ops->fix_hypercall(ctxt); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3319 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3320 | if (rc != X86EMUL_CONTINUE) |
| 3321 | return rc; |
| 3322 | |
| 3323 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3324 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3325 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3326 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3327 | return X86EMUL_CONTINUE; |
| 3328 | } |
| 3329 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3330 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3331 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3332 | struct desc_ptr *ptr)) |
| 3333 | { |
| 3334 | struct desc_ptr desc_ptr; |
| 3335 | |
| 3336 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3337 | ctxt->op_bytes = 8; |
| 3338 | get(ctxt, &desc_ptr); |
| 3339 | if (ctxt->op_bytes == 2) { |
| 3340 | ctxt->op_bytes = 4; |
| 3341 | desc_ptr.address &= 0x00ffffff; |
| 3342 | } |
| 3343 | /* Disable writeback. */ |
| 3344 | ctxt->dst.type = OP_NONE; |
| 3345 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3346 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3347 | } |
| 3348 | |
| 3349 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3350 | { |
| 3351 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3352 | } |
| 3353 | |
| 3354 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3355 | { |
| 3356 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3357 | } |
| 3358 | |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3359 | static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3360 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3361 | struct desc_ptr desc_ptr; |
| 3362 | int rc; |
| 3363 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3364 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3365 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3366 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3367 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3368 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3369 | if (rc != X86EMUL_CONTINUE) |
| 3370 | return rc; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 3371 | if (ctxt->mode == X86EMUL_MODE_PROT64 && |
| 3372 | is_noncanonical_address(desc_ptr.address)) |
| 3373 | return emulate_gp(ctxt, 0); |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3374 | if (lgdt) |
| 3375 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3376 | else |
| 3377 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3378 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3379 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3380 | return X86EMUL_CONTINUE; |
| 3381 | } |
| 3382 | |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3383 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3384 | { |
| 3385 | return em_lgdt_lidt(ctxt, true); |
| 3386 | } |
| 3387 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3388 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3389 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3390 | int rc; |
| 3391 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3392 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3393 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3394 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3395 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3396 | return rc; |
| 3397 | } |
| 3398 | |
| 3399 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3400 | { |
Nadav Amit | 5b7f6a1e | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3401 | return em_lgdt_lidt(ctxt, false); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3405 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3406 | if (ctxt->dst.type == OP_MEM) |
| 3407 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3408 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3409 | return X86EMUL_CONTINUE; |
| 3410 | } |
| 3411 | |
| 3412 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3413 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3414 | 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] | 3415 | | (ctxt->src.val & 0x0f)); |
| 3416 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3417 | return X86EMUL_CONTINUE; |
| 3418 | } |
| 3419 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3420 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3421 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3422 | int rc = X86EMUL_CONTINUE; |
| 3423 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 3424 | register_address_increment(ctxt, VCPU_REGS_RCX, -1); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3425 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3426 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3427 | rc = jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3428 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3429 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3433 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3434 | int rc = X86EMUL_CONTINUE; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3435 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3436 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
| 3437 | rc = jmp_rel(ctxt, ctxt->src.val); |
| 3438 | |
| 3439 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3440 | } |
| 3441 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3442 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3443 | { |
| 3444 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3445 | &ctxt->dst.val)) |
| 3446 | return X86EMUL_IO_NEEDED; |
| 3447 | |
| 3448 | return X86EMUL_CONTINUE; |
| 3449 | } |
| 3450 | |
| 3451 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3452 | { |
| 3453 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3454 | &ctxt->src.val, 1); |
| 3455 | /* Disable writeback. */ |
| 3456 | ctxt->dst.type = OP_NONE; |
| 3457 | return X86EMUL_CONTINUE; |
| 3458 | } |
| 3459 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3460 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3461 | { |
| 3462 | if (emulator_bad_iopl(ctxt)) |
| 3463 | return emulate_gp(ctxt, 0); |
| 3464 | |
| 3465 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3466 | return X86EMUL_CONTINUE; |
| 3467 | } |
| 3468 | |
| 3469 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3470 | { |
| 3471 | if (emulator_bad_iopl(ctxt)) |
| 3472 | return emulate_gp(ctxt, 0); |
| 3473 | |
| 3474 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3475 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3476 | return X86EMUL_CONTINUE; |
| 3477 | } |
| 3478 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3479 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3480 | { |
| 3481 | u32 eax, ebx, ecx, edx; |
| 3482 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3483 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3484 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3485 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3486 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3487 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3488 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3489 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3490 | return X86EMUL_CONTINUE; |
| 3491 | } |
| 3492 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3493 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3494 | { |
| 3495 | u32 flags; |
| 3496 | |
| 3497 | flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF; |
| 3498 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3499 | |
| 3500 | ctxt->eflags &= ~0xffUL; |
| 3501 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3502 | return X86EMUL_CONTINUE; |
| 3503 | } |
| 3504 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3505 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3506 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3507 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3508 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3509 | return X86EMUL_CONTINUE; |
| 3510 | } |
| 3511 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3512 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3513 | { |
| 3514 | switch (ctxt->op_bytes) { |
| 3515 | #ifdef CONFIG_X86_64 |
| 3516 | case 8: |
| 3517 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3518 | break; |
| 3519 | #endif |
| 3520 | default: |
| 3521 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3522 | break; |
| 3523 | } |
| 3524 | return X86EMUL_CONTINUE; |
| 3525 | } |
| 3526 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3527 | static int em_clflush(struct x86_emulate_ctxt *ctxt) |
| 3528 | { |
| 3529 | /* emulating clflush regardless of cpuid */ |
| 3530 | return X86EMUL_CONTINUE; |
| 3531 | } |
| 3532 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3533 | static bool valid_cr(int nr) |
| 3534 | { |
| 3535 | switch (nr) { |
| 3536 | case 0: |
| 3537 | case 2 ... 4: |
| 3538 | case 8: |
| 3539 | return true; |
| 3540 | default: |
| 3541 | return false; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3546 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3547 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3548 | return emulate_ud(ctxt); |
| 3549 | |
| 3550 | return X86EMUL_CONTINUE; |
| 3551 | } |
| 3552 | |
| 3553 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3554 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3555 | u64 new_val = ctxt->src.val64; |
| 3556 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3557 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3558 | |
| 3559 | static u64 cr_reserved_bits[] = { |
| 3560 | 0xffffffff00000000ULL, |
| 3561 | 0, 0, 0, /* CR3 checked later */ |
| 3562 | CR4_RESERVED_BITS, |
| 3563 | 0, 0, 0, |
| 3564 | CR8_RESERVED_BITS, |
| 3565 | }; |
| 3566 | |
| 3567 | if (!valid_cr(cr)) |
| 3568 | return emulate_ud(ctxt); |
| 3569 | |
| 3570 | if (new_val & cr_reserved_bits[cr]) |
| 3571 | return emulate_gp(ctxt, 0); |
| 3572 | |
| 3573 | switch (cr) { |
| 3574 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3575 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3576 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3577 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3578 | return emulate_gp(ctxt, 0); |
| 3579 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3580 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3581 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3582 | |
| 3583 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3584 | !(cr4 & X86_CR4_PAE)) |
| 3585 | return emulate_gp(ctxt, 0); |
| 3586 | |
| 3587 | break; |
| 3588 | } |
| 3589 | case 3: { |
| 3590 | u64 rsvd = 0; |
| 3591 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3592 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3593 | if (efer & EFER_LMA) |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 3594 | rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3595 | |
| 3596 | if (new_val & rsvd) |
| 3597 | return emulate_gp(ctxt, 0); |
| 3598 | |
| 3599 | break; |
| 3600 | } |
| 3601 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3602 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3603 | |
| 3604 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3605 | return emulate_gp(ctxt, 0); |
| 3606 | |
| 3607 | break; |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | return X86EMUL_CONTINUE; |
| 3612 | } |
| 3613 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3614 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3615 | { |
| 3616 | unsigned long dr7; |
| 3617 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3618 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3619 | |
| 3620 | /* Check if DR7.Global_Enable is set */ |
| 3621 | return dr7 & (1 << 13); |
| 3622 | } |
| 3623 | |
| 3624 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3625 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3626 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3627 | u64 cr4; |
| 3628 | |
| 3629 | if (dr > 7) |
| 3630 | return emulate_ud(ctxt); |
| 3631 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3632 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3633 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3634 | return emulate_ud(ctxt); |
| 3635 | |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3636 | if (check_dr7_gd(ctxt)) { |
| 3637 | ulong dr6; |
| 3638 | |
| 3639 | ctxt->ops->get_dr(ctxt, 6, &dr6); |
| 3640 | dr6 &= ~15; |
| 3641 | dr6 |= DR6_BD | DR6_RTM; |
| 3642 | ctxt->ops->set_dr(ctxt, 6, dr6); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3643 | return emulate_db(ctxt); |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3644 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3645 | |
| 3646 | return X86EMUL_CONTINUE; |
| 3647 | } |
| 3648 | |
| 3649 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3650 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3651 | u64 new_val = ctxt->src.val64; |
| 3652 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3653 | |
| 3654 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3655 | return emulate_gp(ctxt, 0); |
| 3656 | |
| 3657 | return check_dr_read(ctxt); |
| 3658 | } |
| 3659 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3660 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3661 | { |
| 3662 | u64 efer; |
| 3663 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3664 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3665 | |
| 3666 | if (!(efer & EFER_SVME)) |
| 3667 | return emulate_ud(ctxt); |
| 3668 | |
| 3669 | return X86EMUL_CONTINUE; |
| 3670 | } |
| 3671 | |
| 3672 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3673 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3674 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3675 | |
| 3676 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3677 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3678 | return emulate_gp(ctxt, 0); |
| 3679 | |
| 3680 | return check_svme(ctxt); |
| 3681 | } |
| 3682 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3683 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3684 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3685 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3686 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3687 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3688 | return emulate_ud(ctxt); |
| 3689 | |
| 3690 | return X86EMUL_CONTINUE; |
| 3691 | } |
| 3692 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3693 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3694 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3695 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3696 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3697 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3698 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3699 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3700 | return emulate_gp(ctxt, 0); |
| 3701 | |
| 3702 | return X86EMUL_CONTINUE; |
| 3703 | } |
| 3704 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3705 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3706 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3707 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3708 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3709 | return emulate_gp(ctxt, 0); |
| 3710 | |
| 3711 | return X86EMUL_CONTINUE; |
| 3712 | } |
| 3713 | |
| 3714 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3715 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3716 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3717 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3718 | return emulate_gp(ctxt, 0); |
| 3719 | |
| 3720 | return X86EMUL_CONTINUE; |
| 3721 | } |
| 3722 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3723 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3724 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 3725 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 3726 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3727 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3728 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3729 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3730 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3731 | #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3732 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3733 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3734 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3735 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3736 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3737 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3738 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 3739 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3740 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3741 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3742 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3743 | #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] | 3744 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3745 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3746 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3747 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3748 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3749 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3750 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3751 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3752 | |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3753 | static const struct opcode group7_rm0[] = { |
| 3754 | N, |
| 3755 | I(SrcNone | Priv | EmulateOnUD, em_vmcall), |
| 3756 | N, N, N, N, N, N, |
| 3757 | }; |
| 3758 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3759 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3760 | DI(SrcNone | Priv, monitor), |
| 3761 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3762 | N, N, N, N, N, N, |
| 3763 | }; |
| 3764 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3765 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3766 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3767 | II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3768 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3769 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3770 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3771 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3772 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3773 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3774 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3775 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3776 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3777 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3778 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3779 | N, N, N, N, N, N, |
| 3780 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3781 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3782 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3783 | F(Lock, em_add), |
| 3784 | F(Lock | PageTable, em_or), |
| 3785 | F(Lock, em_adc), |
| 3786 | F(Lock, em_sbb), |
| 3787 | F(Lock | PageTable, em_and), |
| 3788 | F(Lock, em_sub), |
| 3789 | F(Lock, em_xor), |
| 3790 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3791 | }; |
| 3792 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3793 | static const struct opcode group1A[] = { |
Nadav Amit | ab70809 | 2014-12-25 02:52:21 +0200 | [diff] [blame] | 3794 | 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] | 3795 | }; |
| 3796 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3797 | static const struct opcode group2[] = { |
| 3798 | F(DstMem | ModRM, em_rol), |
| 3799 | F(DstMem | ModRM, em_ror), |
| 3800 | F(DstMem | ModRM, em_rcl), |
| 3801 | F(DstMem | ModRM, em_rcr), |
| 3802 | F(DstMem | ModRM, em_shl), |
| 3803 | F(DstMem | ModRM, em_shr), |
| 3804 | F(DstMem | ModRM, em_shl), |
| 3805 | F(DstMem | ModRM, em_sar), |
| 3806 | }; |
| 3807 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3808 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3809 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3810 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3811 | F(DstMem | SrcNone | Lock, em_not), |
| 3812 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3813 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3814 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3815 | F(DstXacc | Src2Mem, em_div_ex), |
| 3816 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3817 | }; |
| 3818 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3819 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3820 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3821 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3822 | N, N, N, N, N, N, |
| 3823 | }; |
| 3824 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3825 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3826 | F(DstMem | SrcNone | Lock, em_inc), |
| 3827 | F(DstMem | SrcNone | Lock, em_dec), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3828 | I(SrcMem | NearBranch, em_call_near_abs), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3829 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3830 | I(SrcMem | NearBranch, em_jmp_abs), |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 3831 | I(SrcMemFAddr | ImplicitOps, em_jmp_far), |
| 3832 | I(SrcMem | Stack, em_push), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3833 | }; |
| 3834 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3835 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3836 | DI(Prot, sldt), |
| 3837 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3838 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3839 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3840 | N, N, N, N, |
| 3841 | }; |
| 3842 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3843 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3844 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3845 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3846 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3847 | II(SrcMem | Priv, em_lidt, lidt), |
| 3848 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3849 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3850 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3851 | }, { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3852 | EXT(0, group7_rm0), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3853 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3854 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3855 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3856 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3857 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3858 | } }; |
| 3859 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3860 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3861 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3862 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3863 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3864 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3865 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3866 | }; |
| 3867 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3868 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3869 | 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] | 3870 | }, { |
| 3871 | N, N, N, N, N, N, N, N, |
| 3872 | } }; |
| 3873 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3874 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3875 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3876 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3877 | }; |
| 3878 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3879 | static const struct gprefix pfx_0f_ae_7 = { |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 3880 | I(SrcMem | ByteOp, em_clflush), N, N, N, |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3881 | }; |
| 3882 | |
| 3883 | static const struct group_dual group15 = { { |
| 3884 | N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7), |
| 3885 | }, { |
| 3886 | N, N, N, N, N, N, N, N, |
| 3887 | } }; |
| 3888 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3889 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3890 | 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] | 3891 | }; |
| 3892 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3893 | static const struct instr_dual instr_dual_0f_2b = { |
| 3894 | I(0, em_mov), N |
| 3895 | }; |
| 3896 | |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 3897 | static const struct gprefix pfx_0f_2b = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3898 | 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] | 3899 | }; |
| 3900 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3901 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3902 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3903 | }; |
| 3904 | |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 3905 | static const struct gprefix pfx_0f_e7 = { |
| 3906 | N, I(Sse, em_mov), N, N, |
| 3907 | }; |
| 3908 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3909 | static const struct escape escape_d9 = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3910 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3911 | }, { |
| 3912 | /* 0xC0 - 0xC7 */ |
| 3913 | N, N, N, N, N, N, N, N, |
| 3914 | /* 0xC8 - 0xCF */ |
| 3915 | N, N, N, N, N, N, N, N, |
| 3916 | /* 0xD0 - 0xC7 */ |
| 3917 | N, N, N, N, N, N, N, N, |
| 3918 | /* 0xD8 - 0xDF */ |
| 3919 | N, N, N, N, N, N, N, N, |
| 3920 | /* 0xE0 - 0xE7 */ |
| 3921 | N, N, N, N, N, N, N, N, |
| 3922 | /* 0xE8 - 0xEF */ |
| 3923 | N, N, N, N, N, N, N, N, |
| 3924 | /* 0xF0 - 0xF7 */ |
| 3925 | N, N, N, N, N, N, N, N, |
| 3926 | /* 0xF8 - 0xFF */ |
| 3927 | N, N, N, N, N, N, N, N, |
| 3928 | } }; |
| 3929 | |
| 3930 | static const struct escape escape_db = { { |
| 3931 | N, N, N, N, N, N, N, N, |
| 3932 | }, { |
| 3933 | /* 0xC0 - 0xC7 */ |
| 3934 | N, N, N, N, N, N, N, N, |
| 3935 | /* 0xC8 - 0xCF */ |
| 3936 | N, N, N, N, N, N, N, N, |
| 3937 | /* 0xD0 - 0xC7 */ |
| 3938 | N, N, N, N, N, N, N, N, |
| 3939 | /* 0xD8 - 0xDF */ |
| 3940 | N, N, N, N, N, N, N, N, |
| 3941 | /* 0xE0 - 0xE7 */ |
| 3942 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3943 | /* 0xE8 - 0xEF */ |
| 3944 | N, N, N, N, N, N, N, N, |
| 3945 | /* 0xF0 - 0xF7 */ |
| 3946 | N, N, N, N, N, N, N, N, |
| 3947 | /* 0xF8 - 0xFF */ |
| 3948 | N, N, N, N, N, N, N, N, |
| 3949 | } }; |
| 3950 | |
| 3951 | static const struct escape escape_dd = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3952 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3953 | }, { |
| 3954 | /* 0xC0 - 0xC7 */ |
| 3955 | N, N, N, N, N, N, N, N, |
| 3956 | /* 0xC8 - 0xCF */ |
| 3957 | N, N, N, N, N, N, N, N, |
| 3958 | /* 0xD0 - 0xC7 */ |
| 3959 | N, N, N, N, N, N, N, N, |
| 3960 | /* 0xD8 - 0xDF */ |
| 3961 | N, N, N, N, N, N, N, N, |
| 3962 | /* 0xE0 - 0xE7 */ |
| 3963 | N, N, N, N, N, N, N, N, |
| 3964 | /* 0xE8 - 0xEF */ |
| 3965 | N, N, N, N, N, N, N, N, |
| 3966 | /* 0xF0 - 0xF7 */ |
| 3967 | N, N, N, N, N, N, N, N, |
| 3968 | /* 0xF8 - 0xFF */ |
| 3969 | N, N, N, N, N, N, N, N, |
| 3970 | } }; |
| 3971 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3972 | static const struct instr_dual instr_dual_0f_c3 = { |
| 3973 | I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N |
| 3974 | }; |
| 3975 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3976 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3977 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3978 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3979 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3980 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3981 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3982 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3983 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3984 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3985 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3986 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3987 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3988 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3989 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3990 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3991 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3992 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3993 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3994 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3995 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3996 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3997 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3998 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3999 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4000 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4001 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 4002 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4003 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 4004 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4005 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 4006 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4007 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 4008 | I(ImplicitOps | Stack | No64, em_pusha), |
| 4009 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4010 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 4011 | N, N, N, N, |
| 4012 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 4013 | I(SrcImm | Mov | Stack, em_push), |
| 4014 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 4015 | I(SrcImmByte | Mov | Stack, em_push), |
| 4016 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4017 | 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] | 4018 | 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] | 4019 | /* 0x70 - 0x7F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4020 | X16(D(SrcImmByte | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4021 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 4022 | G(ByteOp | DstMem | SrcImm, group1), |
| 4023 | G(DstMem | SrcImm, group1), |
| 4024 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 4025 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4026 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4027 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4028 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4029 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4030 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4031 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 4032 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 4033 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 4034 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4035 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4036 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4037 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 4038 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 4039 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 4040 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 4041 | II(ImplicitOps | Stack, em_popf, popf), |
| 4042 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4043 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4044 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4045 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4046 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4047 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4048 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4049 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4050 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 4051 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4052 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4053 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4054 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4055 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4056 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4057 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4058 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4059 | I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm), |
| 4060 | I(ImplicitOps | NearBranch, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4061 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 4062 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4063 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4064 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 4065 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Nadav Amit | 16794aa | 2015-01-26 09:32:22 +0200 | [diff] [blame] | 4066 | I(ImplicitOps | SrcImmU16, em_ret_far_imm), |
| 4067 | I(ImplicitOps, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4068 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4069 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4070 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4071 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 4072 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 4073 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 4074 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 4075 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4076 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4077 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4078 | 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] | 4079 | /* 0xE0 - 0xE7 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4080 | X3(I(SrcImmByte | NearBranch, em_loop)), |
| 4081 | I(SrcImmByte | NearBranch, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4082 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 4083 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4084 | /* 0xE8 - 0xEF */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4085 | I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch), |
| 4086 | I(SrcImmFAddr | No64, em_jmp_far), |
| 4087 | D(SrcImmByte | ImplicitOps | NearBranch), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4088 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 4089 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4090 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4091 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4092 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 4093 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4094 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 4095 | D(ImplicitOps), D(ImplicitOps), |
| 4096 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4097 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 4098 | }; |
| 4099 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4100 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4101 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4102 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4103 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4104 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4105 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4106 | N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4107 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4108 | N, N, N, N, N, N, N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4109 | D(ImplicitOps | ModRM | SrcMem | NoAccess), |
| 4110 | N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4111 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 4112 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 4113 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 4114 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 4115 | check_cr_write), |
| 4116 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 4117 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4118 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4119 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 4120 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4121 | N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4122 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4123 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4124 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4125 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4126 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4127 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4128 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 4129 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4130 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4131 | N, N, N, N, N, N, N, N, |
| 4132 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4133 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4134 | /* 0x50 - 0x5F */ |
| 4135 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4136 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4137 | N, N, N, N, |
| 4138 | N, N, N, N, |
| 4139 | N, N, N, N, |
| 4140 | 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] | 4141 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4142 | N, N, N, N, |
| 4143 | N, N, N, N, |
| 4144 | N, N, N, N, |
| 4145 | 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] | 4146 | /* 0x80 - 0x8F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4147 | X16(D(SrcImm | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4148 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4149 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4150 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4151 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4152 | II(ImplicitOps, em_cpuid, cpuid), |
| 4153 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4154 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4155 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4156 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4157 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4158 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4159 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4160 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4161 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4162 | GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4163 | /* 0xB0 - 0xB7 */ |
Nadav Amit | 2fcf5c8 | 2015-01-26 09:32:21 +0200 | [diff] [blame] | 4164 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4165 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4166 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4167 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4168 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4169 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4170 | /* 0xB8 - 0xBF */ |
| 4171 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4172 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4173 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 4174 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4175 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4176 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 4177 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4178 | N, ID(0, &instr_dual_0f_c3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4179 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4180 | /* 0xC8 - 0xCF */ |
| 4181 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4182 | /* 0xD0 - 0xDF */ |
| 4183 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4184 | /* 0xE0 - 0xEF */ |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4185 | N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), |
| 4186 | N, N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4187 | /* 0xF0 - 0xFF */ |
| 4188 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4189 | }; |
| 4190 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4191 | static const struct instr_dual instr_dual_0f_38_f0 = { |
| 4192 | I(DstReg | SrcMem | Mov, em_movbe), N |
| 4193 | }; |
| 4194 | |
| 4195 | static const struct instr_dual instr_dual_0f_38_f1 = { |
| 4196 | I(DstMem | SrcReg | Mov, em_movbe), N |
| 4197 | }; |
| 4198 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4199 | static const struct gprefix three_byte_0f_38_f0 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4200 | ID(0, &instr_dual_0f_38_f0), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4201 | }; |
| 4202 | |
| 4203 | static const struct gprefix three_byte_0f_38_f1 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4204 | ID(0, &instr_dual_0f_38_f1), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4205 | }; |
| 4206 | |
| 4207 | /* |
| 4208 | * Insns below are selected by the prefix which indexed by the third opcode |
| 4209 | * byte. |
| 4210 | */ |
| 4211 | static const struct opcode opcode_map_0f_38[256] = { |
| 4212 | /* 0x00 - 0x7f */ |
| 4213 | 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] | 4214 | /* 0x80 - 0xef */ |
| 4215 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 4216 | /* 0xf0 - 0xf1 */ |
Nadav Amit | 53bb4f7 | 2014-12-07 11:49:42 +0200 | [diff] [blame] | 4217 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), |
| 4218 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1), |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4219 | /* 0xf2 - 0xff */ |
| 4220 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4221 | }; |
| 4222 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4223 | #undef D |
| 4224 | #undef N |
| 4225 | #undef G |
| 4226 | #undef GD |
| 4227 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4228 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4229 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4230 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4231 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4232 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4233 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4234 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4235 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4236 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4237 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4238 | { |
| 4239 | unsigned size; |
| 4240 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4241 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4242 | if (size == 8) |
| 4243 | size = 4; |
| 4244 | return size; |
| 4245 | } |
| 4246 | |
| 4247 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4248 | unsigned size, bool sign_extension) |
| 4249 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4250 | int rc = X86EMUL_CONTINUE; |
| 4251 | |
| 4252 | op->type = OP_IMM; |
| 4253 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4254 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4255 | /* NB. Immediates are sign-extended as necessary. */ |
| 4256 | switch (op->bytes) { |
| 4257 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4258 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4259 | break; |
| 4260 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4261 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4262 | break; |
| 4263 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4264 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4265 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4266 | case 8: |
| 4267 | op->val = insn_fetch(s64, ctxt); |
| 4268 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4269 | } |
| 4270 | if (!sign_extension) { |
| 4271 | switch (op->bytes) { |
| 4272 | case 1: |
| 4273 | op->val &= 0xff; |
| 4274 | break; |
| 4275 | case 2: |
| 4276 | op->val &= 0xffff; |
| 4277 | break; |
| 4278 | case 4: |
| 4279 | op->val &= 0xffffffff; |
| 4280 | break; |
| 4281 | } |
| 4282 | } |
| 4283 | done: |
| 4284 | return rc; |
| 4285 | } |
| 4286 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4287 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4288 | unsigned d) |
| 4289 | { |
| 4290 | int rc = X86EMUL_CONTINUE; |
| 4291 | |
| 4292 | switch (d) { |
| 4293 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4294 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4295 | break; |
| 4296 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4297 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4298 | break; |
| 4299 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4300 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4301 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4302 | *op = ctxt->memop; |
| 4303 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4304 | if (ctxt->d & BitOp) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4305 | fetch_bit_operand(ctxt); |
| 4306 | op->orig_val = op->val; |
| 4307 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4308 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4309 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4310 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4311 | case OpAcc: |
| 4312 | op->type = OP_REG; |
| 4313 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4314 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4315 | fetch_register_operand(op); |
| 4316 | op->orig_val = op->val; |
| 4317 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4318 | case OpAccLo: |
| 4319 | op->type = OP_REG; |
| 4320 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4321 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4322 | fetch_register_operand(op); |
| 4323 | op->orig_val = op->val; |
| 4324 | break; |
| 4325 | case OpAccHi: |
| 4326 | if (ctxt->d & ByteOp) { |
| 4327 | op->type = OP_NONE; |
| 4328 | break; |
| 4329 | } |
| 4330 | op->type = OP_REG; |
| 4331 | op->bytes = ctxt->op_bytes; |
| 4332 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4333 | fetch_register_operand(op); |
| 4334 | op->orig_val = op->val; |
| 4335 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4336 | case OpDI: |
| 4337 | op->type = OP_MEM; |
| 4338 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4339 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4340 | register_address(ctxt, VCPU_REGS_RDI); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4341 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4342 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4343 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4344 | break; |
| 4345 | case OpDX: |
| 4346 | op->type = OP_REG; |
| 4347 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4348 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4349 | fetch_register_operand(op); |
| 4350 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4351 | case OpCL: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4352 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4353 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4354 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4355 | break; |
| 4356 | case OpImmByte: |
| 4357 | rc = decode_imm(ctxt, op, 1, true); |
| 4358 | break; |
| 4359 | case OpOne: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4360 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4361 | op->bytes = 1; |
| 4362 | op->val = 1; |
| 4363 | break; |
| 4364 | case OpImm: |
| 4365 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4366 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4367 | case OpImm64: |
| 4368 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4369 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4370 | case OpMem8: |
| 4371 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4372 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4373 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4374 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4375 | fetch_register_operand(&ctxt->memop); |
| 4376 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4377 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4378 | case OpMem16: |
| 4379 | ctxt->memop.bytes = 2; |
| 4380 | goto mem_common; |
| 4381 | case OpMem32: |
| 4382 | ctxt->memop.bytes = 4; |
| 4383 | goto mem_common; |
| 4384 | case OpImmU16: |
| 4385 | rc = decode_imm(ctxt, op, 2, false); |
| 4386 | break; |
| 4387 | case OpImmU: |
| 4388 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4389 | break; |
| 4390 | case OpSI: |
| 4391 | op->type = OP_MEM; |
| 4392 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4393 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4394 | register_address(ctxt, VCPU_REGS_RSI); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4395 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4396 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4397 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4398 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4399 | case OpXLat: |
| 4400 | op->type = OP_MEM; |
| 4401 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4402 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4403 | address_mask(ctxt, |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4404 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4405 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4406 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4407 | op->val = 0; |
| 4408 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4409 | case OpImmFAddr: |
| 4410 | op->type = OP_IMM; |
| 4411 | op->addr.mem.ea = ctxt->_eip; |
| 4412 | op->bytes = ctxt->op_bytes + 2; |
| 4413 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4414 | break; |
| 4415 | case OpMemFAddr: |
| 4416 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4417 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4418 | case OpES: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4419 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4420 | op->val = VCPU_SREG_ES; |
| 4421 | break; |
| 4422 | case OpCS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4423 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4424 | op->val = VCPU_SREG_CS; |
| 4425 | break; |
| 4426 | case OpSS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4427 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4428 | op->val = VCPU_SREG_SS; |
| 4429 | break; |
| 4430 | case OpDS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4431 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4432 | op->val = VCPU_SREG_DS; |
| 4433 | break; |
| 4434 | case OpFS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4435 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4436 | op->val = VCPU_SREG_FS; |
| 4437 | break; |
| 4438 | case OpGS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4439 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4440 | op->val = VCPU_SREG_GS; |
| 4441 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4442 | case OpImplicit: |
| 4443 | /* Special instructions do their own operand decoding. */ |
| 4444 | default: |
| 4445 | op->type = OP_NONE; /* Disable writeback. */ |
| 4446 | break; |
| 4447 | } |
| 4448 | |
| 4449 | done: |
| 4450 | return rc; |
| 4451 | } |
| 4452 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4453 | 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] | 4454 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4455 | int rc = X86EMUL_CONTINUE; |
| 4456 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4457 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4458 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4459 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4460 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4461 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4462 | ctxt->memop.type = OP_NONE; |
| 4463 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4464 | ctxt->_eip = ctxt->eip; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 4465 | ctxt->fetch.ptr = ctxt->fetch.data; |
| 4466 | ctxt->fetch.end = ctxt->fetch.data + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4467 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4468 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4469 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4470 | else { |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 4471 | rc = __do_insn_fetch_bytes(ctxt, 1); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4472 | if (rc != X86EMUL_CONTINUE) |
| 4473 | return rc; |
| 4474 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4475 | |
| 4476 | switch (mode) { |
| 4477 | case X86EMUL_MODE_REAL: |
| 4478 | case X86EMUL_MODE_VM86: |
| 4479 | case X86EMUL_MODE_PROT16: |
| 4480 | def_op_bytes = def_ad_bytes = 2; |
| 4481 | break; |
| 4482 | case X86EMUL_MODE_PROT32: |
| 4483 | def_op_bytes = def_ad_bytes = 4; |
| 4484 | break; |
| 4485 | #ifdef CONFIG_X86_64 |
| 4486 | case X86EMUL_MODE_PROT64: |
| 4487 | def_op_bytes = 4; |
| 4488 | def_ad_bytes = 8; |
| 4489 | break; |
| 4490 | #endif |
| 4491 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4492 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4493 | } |
| 4494 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4495 | ctxt->op_bytes = def_op_bytes; |
| 4496 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4497 | |
| 4498 | /* Legacy prefixes. */ |
| 4499 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4500 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4501 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4502 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4503 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4504 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4505 | break; |
| 4506 | case 0x67: /* address-size override */ |
| 4507 | if (mode == X86EMUL_MODE_PROT64) |
| 4508 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4509 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4510 | else |
| 4511 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4512 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4513 | break; |
| 4514 | case 0x26: /* ES override */ |
| 4515 | case 0x2e: /* CS override */ |
| 4516 | case 0x36: /* SS override */ |
| 4517 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4518 | has_seg_override = true; |
| 4519 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4520 | break; |
| 4521 | case 0x64: /* FS override */ |
| 4522 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4523 | has_seg_override = true; |
| 4524 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4525 | break; |
| 4526 | case 0x40 ... 0x4f: /* REX */ |
| 4527 | if (mode != X86EMUL_MODE_PROT64) |
| 4528 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4529 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4530 | continue; |
| 4531 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4532 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4533 | break; |
| 4534 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4535 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4536 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4537 | break; |
| 4538 | default: |
| 4539 | goto done_prefixes; |
| 4540 | } |
| 4541 | |
| 4542 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4543 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4544 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | done_prefixes: |
| 4548 | |
| 4549 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4550 | if (ctxt->rex_prefix & 8) |
| 4551 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4552 | |
| 4553 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4554 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4555 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4556 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4557 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4558 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4559 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4560 | |
| 4561 | /* 0F_38 opcode map */ |
| 4562 | if (ctxt->b == 0x38) { |
| 4563 | ctxt->opcode_len = 3; |
| 4564 | ctxt->b = insn_fetch(u8, ctxt); |
| 4565 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4566 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4567 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4568 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4569 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4570 | if (ctxt->d & ModRM) |
| 4571 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4572 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4573 | /* vex-prefix instructions are not implemented */ |
| 4574 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
Nadav Amit | d14cb5d | 2014-11-02 11:54:58 +0200 | [diff] [blame] | 4575 | (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) { |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4576 | ctxt->d = NotImpl; |
| 4577 | } |
| 4578 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4579 | while (ctxt->d & GroupMask) { |
| 4580 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4581 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4582 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4583 | opcode = opcode.u.group[goffset]; |
| 4584 | break; |
| 4585 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4586 | goffset = (ctxt->modrm >> 3) & 7; |
| 4587 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4588 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4589 | else |
| 4590 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4591 | break; |
| 4592 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4593 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4594 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4595 | break; |
| 4596 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4597 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4598 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4599 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4600 | switch (simd_prefix) { |
| 4601 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4602 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4603 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4604 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4605 | } |
| 4606 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4607 | case Escape: |
| 4608 | if (ctxt->modrm > 0xbf) |
| 4609 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4610 | else |
| 4611 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4612 | break; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4613 | case InstrDual: |
| 4614 | if ((ctxt->modrm >> 6) == 3) |
| 4615 | opcode = opcode.u.idual->mod3; |
| 4616 | else |
| 4617 | opcode = opcode.u.idual->mod012; |
| 4618 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4619 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4620 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4621 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4622 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4623 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4624 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4625 | } |
| 4626 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 4627 | /* Unrecognised? */ |
| 4628 | if (ctxt->d == 0) |
| 4629 | return EMULATION_FAILED; |
| 4630 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4631 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4632 | |
Nadav Amit | 3a6095a | 2014-08-13 16:50:13 +0300 | [diff] [blame] | 4633 | if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) |
| 4634 | return EMULATION_FAILED; |
| 4635 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4636 | if (unlikely(ctxt->d & |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4637 | (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch| |
| 4638 | No16))) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4639 | /* |
| 4640 | * These are copied unconditionally here, and checked unconditionally |
| 4641 | * in x86_emulate_insn. |
| 4642 | */ |
| 4643 | ctxt->check_perm = opcode.check_perm; |
| 4644 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4645 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4646 | if (ctxt->d & NotImpl) |
| 4647 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4648 | |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4649 | if (mode == X86EMUL_MODE_PROT64) { |
| 4650 | if (ctxt->op_bytes == 4 && (ctxt->d & Stack)) |
| 4651 | ctxt->op_bytes = 8; |
| 4652 | else if (ctxt->d & NearBranch) |
| 4653 | ctxt->op_bytes = 8; |
| 4654 | } |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4655 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4656 | if (ctxt->d & Op3264) { |
| 4657 | if (mode == X86EMUL_MODE_PROT64) |
| 4658 | ctxt->op_bytes = 8; |
| 4659 | else |
| 4660 | ctxt->op_bytes = 4; |
| 4661 | } |
| 4662 | |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4663 | if ((ctxt->d & No16) && ctxt->op_bytes == 2) |
| 4664 | ctxt->op_bytes = 4; |
| 4665 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4666 | if (ctxt->d & Sse) |
| 4667 | ctxt->op_bytes = 16; |
| 4668 | else if (ctxt->d & Mmx) |
| 4669 | ctxt->op_bytes = 8; |
| 4670 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4671 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4672 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4673 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4674 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4675 | if (!has_seg_override) { |
| 4676 | has_seg_override = true; |
| 4677 | ctxt->seg_override = ctxt->modrm_seg; |
| 4678 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4679 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4680 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4681 | if (rc != X86EMUL_CONTINUE) |
| 4682 | goto done; |
| 4683 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4684 | if (!has_seg_override) |
| 4685 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4686 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4687 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4688 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4689 | /* |
| 4690 | * Decode and fetch the source operand: register, memory |
| 4691 | * or immediate. |
| 4692 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4693 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4694 | if (rc != X86EMUL_CONTINUE) |
| 4695 | goto done; |
| 4696 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4697 | /* |
| 4698 | * Decode and fetch the second source operand: register, memory |
| 4699 | * or immediate. |
| 4700 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4701 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4702 | if (rc != X86EMUL_CONTINUE) |
| 4703 | goto done; |
| 4704 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4705 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4706 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4707 | |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 4708 | if (ctxt->rip_relative) |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 4709 | ctxt->memopp->addr.mem.ea = address_mask(ctxt, |
| 4710 | ctxt->memopp->addr.mem.ea + ctxt->_eip); |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4711 | |
Paolo Bonzini | a430c91 | 2014-10-23 14:54:14 +0200 | [diff] [blame] | 4712 | done: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4713 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4714 | } |
| 4715 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4716 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4717 | { |
| 4718 | return ctxt->d & PageTable; |
| 4719 | } |
| 4720 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4721 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4722 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4723 | /* The second termination condition only applies for REPE |
| 4724 | * and REPNE. Test if the repeat string operation prefix is |
| 4725 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4726 | * corresponding termination condition according to: |
| 4727 | * - if REPE/REPZ and ZF = 0 then done |
| 4728 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4729 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4730 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4731 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4732 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4733 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4734 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4735 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4736 | return true; |
| 4737 | |
| 4738 | return false; |
| 4739 | } |
| 4740 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4741 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4742 | { |
| 4743 | bool fault = false; |
| 4744 | |
| 4745 | ctxt->ops->get_fpu(ctxt); |
| 4746 | asm volatile("1: fwait \n\t" |
| 4747 | "2: \n\t" |
| 4748 | ".pushsection .fixup,\"ax\" \n\t" |
| 4749 | "3: \n\t" |
| 4750 | "movb $1, %[fault] \n\t" |
| 4751 | "jmp 2b \n\t" |
| 4752 | ".popsection \n\t" |
| 4753 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4754 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4755 | ctxt->ops->put_fpu(ctxt); |
| 4756 | |
| 4757 | if (unlikely(fault)) |
| 4758 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4759 | |
| 4760 | return X86EMUL_CONTINUE; |
| 4761 | } |
| 4762 | |
| 4763 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4764 | struct operand *op) |
| 4765 | { |
| 4766 | if (op->type == OP_MM) |
| 4767 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4768 | } |
| 4769 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4770 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4771 | { |
| 4772 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4773 | if (!(ctxt->d & ByteOp)) |
| 4774 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4775 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4776 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4777 | [fastop]"+S"(fop) |
| 4778 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4779 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4780 | if (!fop) /* exception is returned in fop variable */ |
| 4781 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4782 | return X86EMUL_CONTINUE; |
| 4783 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4784 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4785 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 4786 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4787 | memset(&ctxt->rip_relative, 0, |
| 4788 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4789 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4790 | ctxt->io_read.pos = 0; |
| 4791 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4792 | ctxt->mem_read.end = 0; |
| 4793 | } |
| 4794 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4795 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4796 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4797 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4798 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4799 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4800 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4801 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4802 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4803 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4804 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4805 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4806 | goto done; |
| 4807 | } |
| 4808 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4809 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4810 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4811 | goto done; |
| 4812 | } |
| 4813 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4814 | if (unlikely(ctxt->d & |
| 4815 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 4816 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4817 | (ctxt->d & Undefined)) { |
| 4818 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4819 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4820 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4821 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4822 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4823 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 4824 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4825 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4826 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4827 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4828 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 4829 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4830 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4831 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4832 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4833 | if (ctxt->d & Mmx) { |
| 4834 | rc = flush_pending_x87_faults(ctxt); |
| 4835 | if (rc != X86EMUL_CONTINUE) |
| 4836 | goto done; |
| 4837 | /* |
| 4838 | * Now that we know the fpu is exception safe, we can fetch |
| 4839 | * operands from it. |
| 4840 | */ |
| 4841 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4842 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4843 | if (!(ctxt->d & Mov)) |
| 4844 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4845 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4846 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4847 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4848 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4849 | X86_ICPT_PRE_EXCEPT); |
| 4850 | if (rc != X86EMUL_CONTINUE) |
| 4851 | goto done; |
| 4852 | } |
| 4853 | |
Nadav Amit | 64a3829 | 2014-12-10 11:19:04 +0200 | [diff] [blame] | 4854 | /* Instruction can only be executed in protected mode */ |
| 4855 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 4856 | rc = emulate_ud(ctxt); |
| 4857 | goto done; |
| 4858 | } |
| 4859 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4860 | /* Privileged instruction can be executed only in CPL=0 */ |
| 4861 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 4862 | if (ctxt->d & PrivUD) |
| 4863 | rc = emulate_ud(ctxt); |
| 4864 | else |
| 4865 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4866 | goto done; |
| 4867 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4868 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4869 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4870 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4871 | rc = ctxt->check_perm(ctxt); |
| 4872 | if (rc != X86EMUL_CONTINUE) |
| 4873 | goto done; |
| 4874 | } |
| 4875 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4876 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4877 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4878 | X86_ICPT_POST_EXCEPT); |
| 4879 | if (rc != X86EMUL_CONTINUE) |
| 4880 | goto done; |
| 4881 | } |
| 4882 | |
| 4883 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 4884 | /* All REP prefixes have the same first termination condition */ |
| 4885 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
| 4886 | ctxt->eip = ctxt->_eip; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4887 | ctxt->eflags &= ~EFLG_RF; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4888 | goto done; |
| 4889 | } |
| 4890 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4891 | } |
| 4892 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4893 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4894 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4895 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4896 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4897 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4898 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4899 | } |
| 4900 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4901 | if (ctxt->src2.type == OP_MEM) { |
| 4902 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4903 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4904 | if (rc != X86EMUL_CONTINUE) |
| 4905 | goto done; |
| 4906 | } |
| 4907 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4908 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4909 | goto special_insn; |
| 4910 | |
| 4911 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4912 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4913 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4914 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4915 | &ctxt->dst.val, ctxt->dst.bytes); |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 4916 | if (rc != X86EMUL_CONTINUE) { |
| 4917 | if (rc == X86EMUL_PROPAGATE_FAULT && |
| 4918 | ctxt->exception.vector == PF_VECTOR) |
| 4919 | ctxt->exception.error_code |= PFERR_WRITE_MASK; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4920 | goto done; |
Nadav Amit | c205fb7 | 2014-12-25 02:52:16 +0200 | [diff] [blame] | 4921 | } |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4922 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4923 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4924 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4925 | special_insn: |
| 4926 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4927 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4928 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4929 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4930 | if (rc != X86EMUL_CONTINUE) |
| 4931 | goto done; |
| 4932 | } |
| 4933 | |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 4934 | if (ctxt->rep_prefix && (ctxt->d & String)) |
| 4935 | ctxt->eflags |= EFLG_RF; |
| 4936 | else |
| 4937 | ctxt->eflags &= ~EFLG_RF; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4938 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4939 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4940 | if (ctxt->d & Fastop) { |
| 4941 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4942 | rc = fastop(ctxt, fop); |
| 4943 | if (rc != X86EMUL_CONTINUE) |
| 4944 | goto done; |
| 4945 | goto writeback; |
| 4946 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4947 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4948 | if (rc != X86EMUL_CONTINUE) |
| 4949 | goto done; |
| 4950 | goto writeback; |
| 4951 | } |
| 4952 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4953 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4954 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4955 | else if (ctxt->opcode_len == 3) |
| 4956 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4957 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4958 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4959 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4960 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4961 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4962 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4963 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4964 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4965 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4966 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4967 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4968 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4969 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4970 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4971 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4972 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 4973 | ctxt->dst.type = OP_NONE; |
| 4974 | else |
| 4975 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4976 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4977 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4978 | switch (ctxt->op_bytes) { |
| 4979 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4980 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4981 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4982 | } |
| 4983 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4984 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4985 | rc = emulate_int(ctxt, 3); |
| 4986 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4987 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4988 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4989 | break; |
| 4990 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4991 | if (ctxt->eflags & EFLG_OF) |
| 4992 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4993 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4994 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4995 | case 0xeb: /* jmp rel short */ |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4996 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4997 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4998 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4999 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 5000 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 5001 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5002 | case 0xf5: /* cmc */ |
| 5003 | /* complement carry flag from eflags reg */ |
| 5004 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5005 | break; |
| 5006 | case 0xf8: /* clc */ |
| 5007 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 5008 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 5009 | case 0xf9: /* stc */ |
| 5010 | ctxt->eflags |= EFLG_CF; |
| 5011 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5012 | case 0xfc: /* cld */ |
| 5013 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5014 | break; |
| 5015 | case 0xfd: /* std */ |
| 5016 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 5017 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5018 | default: |
| 5019 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5020 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5021 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5022 | if (rc != X86EMUL_CONTINUE) |
| 5023 | goto done; |
| 5024 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5025 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 5026 | if (ctxt->d & SrcWrite) { |
| 5027 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 5028 | rc = writeback(ctxt, &ctxt->src); |
| 5029 | if (rc != X86EMUL_CONTINUE) |
| 5030 | goto done; |
| 5031 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 5032 | if (!(ctxt->d & NoWrite)) { |
| 5033 | rc = writeback(ctxt, &ctxt->dst); |
| 5034 | if (rc != X86EMUL_CONTINUE) |
| 5035 | goto done; |
| 5036 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5037 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5038 | /* |
| 5039 | * restore dst type in case the decoding will be reused |
| 5040 | * (happens for string instruction ) |
| 5041 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5042 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5043 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5044 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5045 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 5046 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5047 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5048 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 5049 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5050 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5051 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5052 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5053 | if ((ctxt->d & SrcMask) == SrcSI) |
| 5054 | count = ctxt->src.count; |
| 5055 | else |
| 5056 | count = ctxt->dst.count; |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 5057 | register_address_increment(ctxt, VCPU_REGS_RCX, -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5058 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5059 | if (!string_insn_completed(ctxt)) { |
| 5060 | /* |
| 5061 | * Re-enter guest when pio read ahead buffer is empty |
| 5062 | * or, if it is not used, after each 1024 iteration. |
| 5063 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5064 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5065 | (r->end == 0 || r->end != r->pos)) { |
| 5066 | /* |
| 5067 | * Reset read cache. Usually happens before |
| 5068 | * decode, but since instruction is restarted |
| 5069 | * we have to do it here. |
| 5070 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5071 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5072 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5073 | return EMULATION_RESTART; |
| 5074 | } |
| 5075 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 5076 | } |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 5077 | ctxt->eflags &= ~EFLG_RF; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5078 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5079 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5080 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5081 | |
| 5082 | done: |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5083 | if (rc == X86EMUL_PROPAGATE_FAULT) { |
| 5084 | WARN_ON(ctxt->exception.vector > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 5085 | ctxt->have_exception = true; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5086 | } |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 5087 | if (rc == X86EMUL_INTERCEPTED) |
| 5088 | return EMULATION_INTERCEPTED; |
| 5089 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5090 | if (rc == X86EMUL_CONTINUE) |
| 5091 | writeback_registers(ctxt); |
| 5092 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5093 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5094 | |
| 5095 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5096 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5097 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 5098 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5099 | break; |
| 5100 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5101 | case 0x0d: /* GrpP (prefetch) */ |
| 5102 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 5103 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5104 | break; |
| 5105 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5106 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5107 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5108 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5109 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5110 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5111 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 5112 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 5113 | ctxt->dst.val = ctxt->src.val; |
| 5114 | else if (ctxt->mode != X86EMUL_MODE_PROT64 || |
| 5115 | ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5116 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5117 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5118 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5119 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5120 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5121 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5122 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5123 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5124 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5125 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5126 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5127 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5128 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5129 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5130 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5131 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5132 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5133 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5134 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5135 | default: |
| 5136 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5137 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5138 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5139 | threebyte_insn: |
| 5140 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5141 | if (rc != X86EMUL_CONTINUE) |
| 5142 | goto done; |
| 5143 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5144 | goto writeback; |
| 5145 | |
| 5146 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 5147 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5148 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5149 | |
| 5150 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5151 | { |
| 5152 | invalidate_registers(ctxt); |
| 5153 | } |
| 5154 | |
| 5155 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5156 | { |
| 5157 | writeback_registers(ctxt); |
| 5158 | } |