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 | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 89 | #define DstMem16 (OpMem16 << DstShift) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 90 | #define DstImmUByte (OpImmUByte << DstShift) |
| 91 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 92 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 93 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 94 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 95 | #define SrcShift 6 |
| 96 | #define SrcNone (OpNone << SrcShift) |
| 97 | #define SrcReg (OpReg << SrcShift) |
| 98 | #define SrcMem (OpMem << SrcShift) |
| 99 | #define SrcMem16 (OpMem16 << SrcShift) |
| 100 | #define SrcMem32 (OpMem32 << SrcShift) |
| 101 | #define SrcImm (OpImm << SrcShift) |
| 102 | #define SrcImmByte (OpImmByte << SrcShift) |
| 103 | #define SrcOne (OpOne << SrcShift) |
| 104 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 105 | #define SrcImmU (OpImmU << SrcShift) |
| 106 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 107 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 108 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 109 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 110 | #define SrcAcc (OpAcc << SrcShift) |
| 111 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 112 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 113 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 114 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 115 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 116 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 117 | #define BitOp (1<<11) |
| 118 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 119 | #define String (1<<13) /* String instruction (rep capable) */ |
| 120 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 121 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 122 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 123 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 124 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 125 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 126 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 127 | #define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */ |
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 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 173 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 174 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 175 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 176 | #define X2(x...) x, x |
| 177 | #define X3(x...) X2(x), x |
| 178 | #define X4(x...) X2(x), X2(x) |
| 179 | #define X5(x...) X4(x), x |
| 180 | #define X6(x...) X4(x), X2(x) |
| 181 | #define X7(x...) X4(x), X3(x) |
| 182 | #define X8(x...) X4(x), X4(x) |
| 183 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 184 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 185 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 186 | #define FASTOP_SIZE 8 |
| 187 | |
| 188 | /* |
| 189 | * fastop functions have a special calling convention: |
| 190 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 191 | * dst: rax (in/out) |
| 192 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 193 | * src2: rcx (in) |
| 194 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 195 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 196 | * |
| 197 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 198 | * different operand sizes can be reached by calculation, rather than a jump |
| 199 | * table (which would be bigger than the code). |
| 200 | * |
| 201 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 202 | * so they can't be called from C directly. |
| 203 | */ |
| 204 | |
| 205 | struct fastop; |
| 206 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 207 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 208 | u64 flags : 56; |
| 209 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 210 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 211 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 212 | const struct opcode *group; |
| 213 | const struct group_dual *gdual; |
| 214 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 215 | const struct escape *esc; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 216 | const struct instr_dual *idual; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 217 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 218 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 219 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | struct group_dual { |
| 223 | struct opcode mod012[8]; |
| 224 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 225 | }; |
| 226 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 227 | struct gprefix { |
| 228 | struct opcode pfx_no; |
| 229 | struct opcode pfx_66; |
| 230 | struct opcode pfx_f2; |
| 231 | struct opcode pfx_f3; |
| 232 | }; |
| 233 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 234 | struct escape { |
| 235 | struct opcode op[8]; |
| 236 | struct opcode high[64]; |
| 237 | }; |
| 238 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 239 | struct instr_dual { |
| 240 | struct opcode mod012; |
| 241 | struct opcode mod3; |
| 242 | }; |
| 243 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 245 | #define EFLG_ID (1<<21) |
| 246 | #define EFLG_VIP (1<<20) |
| 247 | #define EFLG_VIF (1<<19) |
| 248 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 249 | #define EFLG_VM (1<<17) |
| 250 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 251 | #define EFLG_IOPL (3<<12) |
| 252 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 253 | #define EFLG_OF (1<<11) |
| 254 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 255 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 256 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 257 | #define EFLG_SF (1<<7) |
| 258 | #define EFLG_ZF (1<<6) |
| 259 | #define EFLG_AF (1<<4) |
| 260 | #define EFLG_PF (1<<2) |
| 261 | #define EFLG_CF (1<<0) |
| 262 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 263 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 264 | #define EFLG_RESERVED_ONE_MASK 2 |
| 265 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 266 | enum x86_transfer_type { |
| 267 | X86_TRANSFER_NONE, |
| 268 | X86_TRANSFER_CALL_JMP, |
| 269 | X86_TRANSFER_RET, |
| 270 | X86_TRANSFER_TASK_SWITCH, |
| 271 | }; |
| 272 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 273 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 274 | { |
| 275 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 276 | ctxt->regs_valid |= 1 << nr; |
| 277 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 278 | } |
| 279 | return ctxt->_regs[nr]; |
| 280 | } |
| 281 | |
| 282 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 283 | { |
| 284 | ctxt->regs_valid |= 1 << nr; |
| 285 | ctxt->regs_dirty |= 1 << nr; |
| 286 | return &ctxt->_regs[nr]; |
| 287 | } |
| 288 | |
| 289 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 290 | { |
| 291 | reg_read(ctxt, nr); |
| 292 | return reg_write(ctxt, nr); |
| 293 | } |
| 294 | |
| 295 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 296 | { |
| 297 | unsigned reg; |
| 298 | |
| 299 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 300 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 301 | } |
| 302 | |
| 303 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 304 | { |
| 305 | ctxt->regs_dirty = 0; |
| 306 | ctxt->regs_valid = 0; |
| 307 | } |
| 308 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 309 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 310 | * These EFLAGS bits are restored from saved value during emulation, and |
| 311 | * any changes are written back to the saved value after emulation. |
| 312 | */ |
| 313 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 314 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 315 | #ifdef CONFIG_X86_64 |
| 316 | #define ON64(x) x |
| 317 | #else |
| 318 | #define ON64(x) |
| 319 | #endif |
| 320 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 321 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 322 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 323 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 324 | #define FOP_RET "ret \n\t" |
| 325 | |
| 326 | #define FOP_START(op) \ |
| 327 | extern void em_##op(struct fastop *fake); \ |
| 328 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 329 | ".global em_" #op " \n\t" \ |
| 330 | FOP_ALIGN \ |
| 331 | "em_" #op ": \n\t" |
| 332 | |
| 333 | #define FOP_END \ |
| 334 | ".popsection") |
| 335 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 336 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 337 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 338 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 339 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 340 | |
| 341 | #define FOP1EEX(op, dst) \ |
| 342 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 343 | |
| 344 | #define FASTOP1(op) \ |
| 345 | FOP_START(op) \ |
| 346 | FOP1E(op##b, al) \ |
| 347 | FOP1E(op##w, ax) \ |
| 348 | FOP1E(op##l, eax) \ |
| 349 | ON64(FOP1E(op##q, rax)) \ |
| 350 | FOP_END |
| 351 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 352 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 353 | #define FASTOP1SRC2(op, name) \ |
| 354 | FOP_START(name) \ |
| 355 | FOP1E(op, cl) \ |
| 356 | FOP1E(op, cx) \ |
| 357 | FOP1E(op, ecx) \ |
| 358 | ON64(FOP1E(op, rcx)) \ |
| 359 | FOP_END |
| 360 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 361 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 362 | #define FASTOP1SRC2EX(op, name) \ |
| 363 | FOP_START(name) \ |
| 364 | FOP1EEX(op, cl) \ |
| 365 | FOP1EEX(op, cx) \ |
| 366 | FOP1EEX(op, ecx) \ |
| 367 | ON64(FOP1EEX(op, rcx)) \ |
| 368 | FOP_END |
| 369 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 370 | #define FOP2E(op, dst, src) \ |
| 371 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 372 | |
| 373 | #define FASTOP2(op) \ |
| 374 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 375 | FOP2E(op##b, al, dl) \ |
| 376 | FOP2E(op##w, ax, dx) \ |
| 377 | FOP2E(op##l, eax, edx) \ |
| 378 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 379 | FOP_END |
| 380 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 381 | /* 2 operand, word only */ |
| 382 | #define FASTOP2W(op) \ |
| 383 | FOP_START(op) \ |
| 384 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 385 | FOP2E(op##w, ax, dx) \ |
| 386 | FOP2E(op##l, eax, edx) \ |
| 387 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 388 | FOP_END |
| 389 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 390 | /* 2 operand, src is CL */ |
| 391 | #define FASTOP2CL(op) \ |
| 392 | FOP_START(op) \ |
| 393 | FOP2E(op##b, al, cl) \ |
| 394 | FOP2E(op##w, ax, cl) \ |
| 395 | FOP2E(op##l, eax, cl) \ |
| 396 | ON64(FOP2E(op##q, rax, cl)) \ |
| 397 | FOP_END |
| 398 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 399 | /* 2 operand, src and dest are reversed */ |
| 400 | #define FASTOP2R(op, name) \ |
| 401 | FOP_START(name) \ |
| 402 | FOP2E(op##b, dl, al) \ |
| 403 | FOP2E(op##w, dx, ax) \ |
| 404 | FOP2E(op##l, edx, eax) \ |
| 405 | ON64(FOP2E(op##q, rdx, rax)) \ |
| 406 | FOP_END |
| 407 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 408 | #define FOP3E(op, dst, src, src2) \ |
| 409 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 410 | |
| 411 | /* 3-operand, word-only, src2=cl */ |
| 412 | #define FASTOP3WCL(op) \ |
| 413 | FOP_START(op) \ |
| 414 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 415 | FOP3E(op##w, ax, dx, cl) \ |
| 416 | FOP3E(op##l, eax, edx, cl) \ |
| 417 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 418 | FOP_END |
| 419 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 420 | /* Special case for SETcc - 1 instruction per cc */ |
| 421 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 422 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 423 | asm(".global kvm_fastop_exception \n" |
| 424 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 425 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 426 | FOP_START(setcc) |
| 427 | FOP_SETCC(seto) |
| 428 | FOP_SETCC(setno) |
| 429 | FOP_SETCC(setc) |
| 430 | FOP_SETCC(setnc) |
| 431 | FOP_SETCC(setz) |
| 432 | FOP_SETCC(setnz) |
| 433 | FOP_SETCC(setbe) |
| 434 | FOP_SETCC(setnbe) |
| 435 | FOP_SETCC(sets) |
| 436 | FOP_SETCC(setns) |
| 437 | FOP_SETCC(setp) |
| 438 | FOP_SETCC(setnp) |
| 439 | FOP_SETCC(setl) |
| 440 | FOP_SETCC(setnl) |
| 441 | FOP_SETCC(setle) |
| 442 | FOP_SETCC(setnle) |
| 443 | FOP_END; |
| 444 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 445 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 446 | FOP_END; |
| 447 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 448 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 449 | enum x86_intercept intercept, |
| 450 | enum x86_intercept_stage stage) |
| 451 | { |
| 452 | struct x86_instruction_info info = { |
| 453 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 454 | .rep_prefix = ctxt->rep_prefix, |
| 455 | .modrm_mod = ctxt->modrm_mod, |
| 456 | .modrm_reg = ctxt->modrm_reg, |
| 457 | .modrm_rm = ctxt->modrm_rm, |
| 458 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 459 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 460 | .src_bytes = ctxt->src.bytes, |
| 461 | .dst_bytes = ctxt->dst.bytes, |
| 462 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 463 | .next_rip = ctxt->eip, |
| 464 | }; |
| 465 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 466 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 469 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 470 | { |
| 471 | *dest = (*dest & ~mask) | (src & mask); |
| 472 | } |
| 473 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 474 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 475 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 476 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 479 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 480 | { |
| 481 | u16 sel; |
| 482 | struct desc_struct ss; |
| 483 | |
| 484 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 485 | return ~0UL; |
| 486 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 487 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 488 | } |
| 489 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 490 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 491 | { |
| 492 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 493 | } |
| 494 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 495 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 496 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 497 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 498 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 499 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 500 | return reg; |
| 501 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 502 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static inline unsigned long |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 506 | register_address(struct x86_emulate_ctxt *ctxt, int reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 507 | { |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 508 | return address_mask(ctxt, reg_read(ctxt, reg)); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 511 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 512 | { |
| 513 | assign_masked(reg, *reg + inc, mask); |
| 514 | } |
| 515 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 516 | static inline void |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 517 | register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 518 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 519 | ulong mask; |
| 520 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 521 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 522 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 523 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 524 | mask = ad_mask(ctxt); |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 525 | masked_increment(reg_rmw(ctxt, reg), mask, inc); |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 529 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 530 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 531 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 532 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 533 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 534 | { |
| 535 | u32 limit = get_desc_limit(desc); |
| 536 | |
| 537 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 538 | } |
| 539 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 540 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 541 | { |
| 542 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 543 | return 0; |
| 544 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 545 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 546 | } |
| 547 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 548 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 549 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 550 | { |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 551 | WARN_ON(vec > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 552 | ctxt->exception.vector = vec; |
| 553 | ctxt->exception.error_code = error; |
| 554 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 555 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 556 | } |
| 557 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 558 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 559 | { |
| 560 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 561 | } |
| 562 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 563 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 564 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 565 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 566 | } |
| 567 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 568 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 569 | { |
| 570 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 571 | } |
| 572 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 573 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 574 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 575 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 576 | } |
| 577 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 578 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 579 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 580 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 581 | } |
| 582 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 583 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 584 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 585 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 586 | } |
| 587 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 588 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 589 | { |
| 590 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 591 | } |
| 592 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 593 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 594 | { |
| 595 | u16 selector; |
| 596 | struct desc_struct desc; |
| 597 | |
| 598 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 599 | return selector; |
| 600 | } |
| 601 | |
| 602 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 603 | unsigned seg) |
| 604 | { |
| 605 | u16 dummy; |
| 606 | u32 base3; |
| 607 | struct desc_struct desc; |
| 608 | |
| 609 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 610 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 611 | } |
| 612 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 613 | /* |
| 614 | * x86 defines three classes of vector instructions: explicitly |
| 615 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 616 | * depending on whether they're AVX encoded or not. |
| 617 | * |
| 618 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 619 | * subject to the same check. |
| 620 | */ |
| 621 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 622 | { |
| 623 | if (likely(size < 16)) |
| 624 | return false; |
| 625 | |
| 626 | if (ctxt->d & Aligned) |
| 627 | return true; |
| 628 | else if (ctxt->d & Unaligned) |
| 629 | return false; |
| 630 | else if (ctxt->d & Avx) |
| 631 | return false; |
| 632 | else |
| 633 | return true; |
| 634 | } |
| 635 | |
Paolo Bonzini | d09155d | 2014-10-27 14:54:44 +0100 | [diff] [blame] | 636 | static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, |
| 637 | struct segmented_address addr, |
| 638 | unsigned *max_size, unsigned size, |
| 639 | bool write, bool fetch, |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 640 | enum x86emul_mode mode, ulong *linear) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 641 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 642 | struct desc_struct desc; |
| 643 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 644 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 645 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 646 | u16 sel; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 647 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 648 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 649 | *max_size = 0; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 650 | switch (mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 651 | case X86EMUL_MODE_PROT64: |
Nadav Amit | 4be4de7 | 2014-09-18 22:39:40 +0300 | [diff] [blame] | 652 | if (is_noncanonical_address(la)) |
Nadav Amit | abc7d8a | 2014-11-19 17:43:12 +0200 | [diff] [blame] | 653 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 654 | |
| 655 | *max_size = min_t(u64, ~0u, (1ull << 48) - la); |
| 656 | if (size > *max_size) |
| 657 | goto bad; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 658 | break; |
| 659 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 660 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 661 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 662 | if (!usable) |
| 663 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 664 | /* code segment in protected mode or read-only data segment */ |
| 665 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 666 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 667 | goto bad; |
| 668 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 669 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 670 | goto bad; |
| 671 | lim = desc_limit_scaled(&desc); |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 672 | if (!(desc.type & 8) && (desc.type & 4)) { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 673 | /* expand-down segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 674 | if (addr.ea <= lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 675 | goto bad; |
| 676 | lim = desc.d ? 0xffffffff : 0xffff; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 677 | } |
Paolo Bonzini | 997b041 | 2014-11-19 18:33:38 +0100 | [diff] [blame] | 678 | if (addr.ea > lim) |
| 679 | goto bad; |
| 680 | *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea); |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 681 | if (size > *max_size) |
| 682 | goto bad; |
Nadav Amit | 31ff648 | 2014-11-19 17:43:13 +0200 | [diff] [blame] | 683 | la &= (u32)-1; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 684 | break; |
| 685 | } |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 686 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 687 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 688 | *linear = la; |
| 689 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 690 | bad: |
| 691 | if (addr.seg == VCPU_SREG_SS) |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 692 | return emulate_ss(ctxt, 0); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 693 | else |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 694 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 695 | } |
| 696 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 697 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 698 | struct segmented_address addr, |
| 699 | unsigned size, bool write, |
| 700 | ulong *linear) |
| 701 | { |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 702 | unsigned max_size; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 703 | return __linearize(ctxt, addr, &max_size, size, write, false, |
| 704 | ctxt->mode, linear); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 705 | } |
| 706 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 707 | static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 708 | enum x86emul_mode mode) |
| 709 | { |
| 710 | ulong linear; |
| 711 | int rc; |
| 712 | unsigned max_size; |
| 713 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 714 | .ea = dst }; |
| 715 | |
| 716 | if (ctxt->op_bytes != sizeof(unsigned long)) |
| 717 | addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1); |
| 718 | rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear); |
| 719 | if (rc == X86EMUL_CONTINUE) |
| 720 | ctxt->_eip = addr.ea; |
| 721 | return rc; |
| 722 | } |
| 723 | |
| 724 | static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst) |
| 725 | { |
| 726 | return assign_eip(ctxt, dst, ctxt->mode); |
| 727 | } |
| 728 | |
| 729 | static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 730 | const struct desc_struct *cs_desc) |
| 731 | { |
| 732 | enum x86emul_mode mode = ctxt->mode; |
| 733 | |
| 734 | #ifdef CONFIG_X86_64 |
| 735 | if (ctxt->mode >= X86EMUL_MODE_PROT32 && cs_desc->l) { |
| 736 | u64 efer = 0; |
| 737 | |
| 738 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 739 | if (efer & EFER_LMA) |
| 740 | mode = X86EMUL_MODE_PROT64; |
| 741 | } |
| 742 | #endif |
| 743 | if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32) |
| 744 | mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 745 | return assign_eip(ctxt, dst, mode); |
| 746 | } |
| 747 | |
| 748 | static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
| 749 | { |
| 750 | return assign_eip_near(ctxt, ctxt->_eip + rel); |
| 751 | } |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 752 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 753 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 754 | struct segmented_address addr, |
| 755 | void *data, |
| 756 | unsigned size) |
| 757 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 758 | int rc; |
| 759 | ulong linear; |
| 760 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 761 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 762 | if (rc != X86EMUL_CONTINUE) |
| 763 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 764 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 767 | /* |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 768 | * Prefetch the remaining bytes of the instruction without crossing page |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 769 | * boundary if they are not in fetch_cache yet. |
| 770 | */ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 771 | 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] | 772 | { |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 773 | int rc; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 774 | unsigned size, max_size; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 775 | unsigned long linear; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 776 | int cur_size = ctxt->fetch.end - ctxt->fetch.data; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 777 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 778 | .ea = ctxt->eip + cur_size }; |
| 779 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 780 | /* |
| 781 | * We do not know exactly how many bytes will be needed, and |
| 782 | * __linearize is expensive, so fetch as much as possible. We |
| 783 | * just have to avoid going beyond the 15 byte limit, the end |
| 784 | * of the segment, or the end of the page. |
| 785 | * |
| 786 | * __linearize is called with size 0 so that it does not do any |
| 787 | * boundary check itself. Instead, we use max_size to check |
| 788 | * against op_size. |
| 789 | */ |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 790 | rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode, |
| 791 | &linear); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 792 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 793 | return rc; |
| 794 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 795 | size = min_t(unsigned, 15UL ^ cur_size, max_size); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 796 | size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear)); |
Paolo Bonzini | 5cfc7e0 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * One instruction can only straddle two pages, |
| 800 | * and one has been loaded at the beginning of |
| 801 | * x86_decode_insn. So, if not enough bytes |
| 802 | * still, we must have hit the 15-byte boundary. |
| 803 | */ |
| 804 | if (unlikely(size < op_size)) |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 805 | return emulate_gp(ctxt, 0); |
| 806 | |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 807 | rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 808 | size, &ctxt->exception); |
| 809 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 810 | return rc; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 811 | ctxt->fetch.end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 812 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 815 | static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, |
| 816 | unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 817 | { |
Nadav Amit | 08da44a | 2014-10-03 01:10:04 +0300 | [diff] [blame] | 818 | unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr; |
| 819 | |
| 820 | if (unlikely(done_size < size)) |
| 821 | return __do_insn_fetch_bytes(ctxt, size - done_size); |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 822 | else |
| 823 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 826 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 827 | #define insn_fetch(_type, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 828 | ({ _type _x; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 829 | \ |
| 830 | rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 831 | if (rc != X86EMUL_CONTINUE) \ |
| 832 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 833 | ctxt->_eip += sizeof(_type); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 834 | _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \ |
| 835 | ctxt->fetch.ptr += sizeof(_type); \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 836 | _x; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 837 | }) |
| 838 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 839 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 840 | ({ \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 841 | rc = do_insn_fetch_bytes(_ctxt, _size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 842 | if (rc != X86EMUL_CONTINUE) \ |
| 843 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 844 | ctxt->_eip += (_size); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 845 | memcpy(_arr, ctxt->fetch.ptr, _size); \ |
| 846 | ctxt->fetch.ptr += (_size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 847 | }) |
| 848 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 849 | /* |
| 850 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 851 | * pointer into the block that addresses the relevant register. |
| 852 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 853 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 854 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 855 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 856 | { |
| 857 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 858 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 859 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 860 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 861 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 862 | else |
| 863 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 864 | return p; |
| 865 | } |
| 866 | |
| 867 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 868 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 869 | u16 *size, unsigned long *address, int op_bytes) |
| 870 | { |
| 871 | int rc; |
| 872 | |
| 873 | if (op_bytes == 2) |
| 874 | op_bytes = 3; |
| 875 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 876 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 877 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 878 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 879 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 880 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 881 | return rc; |
| 882 | } |
| 883 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 884 | FASTOP2(add); |
| 885 | FASTOP2(or); |
| 886 | FASTOP2(adc); |
| 887 | FASTOP2(sbb); |
| 888 | FASTOP2(and); |
| 889 | FASTOP2(sub); |
| 890 | FASTOP2(xor); |
| 891 | FASTOP2(cmp); |
| 892 | FASTOP2(test); |
| 893 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 894 | FASTOP1SRC2(mul, mul_ex); |
| 895 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 896 | FASTOP1SRC2EX(div, div_ex); |
| 897 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 898 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 899 | FASTOP3WCL(shld); |
| 900 | FASTOP3WCL(shrd); |
| 901 | |
| 902 | FASTOP2W(imul); |
| 903 | |
| 904 | FASTOP1(not); |
| 905 | FASTOP1(neg); |
| 906 | FASTOP1(inc); |
| 907 | FASTOP1(dec); |
| 908 | |
| 909 | FASTOP2CL(rol); |
| 910 | FASTOP2CL(ror); |
| 911 | FASTOP2CL(rcl); |
| 912 | FASTOP2CL(rcr); |
| 913 | FASTOP2CL(shl); |
| 914 | FASTOP2CL(shr); |
| 915 | FASTOP2CL(sar); |
| 916 | |
| 917 | FASTOP2W(bsf); |
| 918 | FASTOP2W(bsr); |
| 919 | FASTOP2W(bt); |
| 920 | FASTOP2W(bts); |
| 921 | FASTOP2W(btr); |
| 922 | FASTOP2W(btc); |
| 923 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 924 | FASTOP2(xadd); |
| 925 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 926 | FASTOP2R(cmp, cmp_r); |
| 927 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 928 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 929 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 930 | u8 rc; |
| 931 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 932 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 933 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 934 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 935 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 936 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 937 | } |
| 938 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 939 | static void fetch_register_operand(struct operand *op) |
| 940 | { |
| 941 | switch (op->bytes) { |
| 942 | case 1: |
| 943 | op->val = *(u8 *)op->addr.reg; |
| 944 | break; |
| 945 | case 2: |
| 946 | op->val = *(u16 *)op->addr.reg; |
| 947 | break; |
| 948 | case 4: |
| 949 | op->val = *(u32 *)op->addr.reg; |
| 950 | break; |
| 951 | case 8: |
| 952 | op->val = *(u64 *)op->addr.reg; |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 957 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 958 | { |
| 959 | ctxt->ops->get_fpu(ctxt); |
| 960 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 961 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 962 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 963 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 964 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 965 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 966 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 967 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 968 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 969 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 970 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 971 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 972 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 973 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 974 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 975 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 976 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 977 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 978 | #endif |
| 979 | default: BUG(); |
| 980 | } |
| 981 | ctxt->ops->put_fpu(ctxt); |
| 982 | } |
| 983 | |
| 984 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 985 | int reg) |
| 986 | { |
| 987 | ctxt->ops->get_fpu(ctxt); |
| 988 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 989 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 990 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 991 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 992 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 993 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 994 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 995 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 996 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 997 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 998 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 999 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1000 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1001 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1002 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1003 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1004 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1005 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1006 | #endif |
| 1007 | default: BUG(); |
| 1008 | } |
| 1009 | ctxt->ops->put_fpu(ctxt); |
| 1010 | } |
| 1011 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1012 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1013 | { |
| 1014 | ctxt->ops->get_fpu(ctxt); |
| 1015 | switch (reg) { |
| 1016 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1017 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1018 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1019 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1020 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1021 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1022 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1023 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1024 | default: BUG(); |
| 1025 | } |
| 1026 | ctxt->ops->put_fpu(ctxt); |
| 1027 | } |
| 1028 | |
| 1029 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1030 | { |
| 1031 | ctxt->ops->get_fpu(ctxt); |
| 1032 | switch (reg) { |
| 1033 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1034 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1035 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1036 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1037 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1038 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1039 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1040 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1041 | default: BUG(); |
| 1042 | } |
| 1043 | ctxt->ops->put_fpu(ctxt); |
| 1044 | } |
| 1045 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1046 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1047 | { |
| 1048 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1049 | return emulate_nm(ctxt); |
| 1050 | |
| 1051 | ctxt->ops->get_fpu(ctxt); |
| 1052 | asm volatile("fninit"); |
| 1053 | ctxt->ops->put_fpu(ctxt); |
| 1054 | return X86EMUL_CONTINUE; |
| 1055 | } |
| 1056 | |
| 1057 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1058 | { |
| 1059 | u16 fcw; |
| 1060 | |
| 1061 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1062 | return emulate_nm(ctxt); |
| 1063 | |
| 1064 | ctxt->ops->get_fpu(ctxt); |
| 1065 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1066 | ctxt->ops->put_fpu(ctxt); |
| 1067 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1068 | ctxt->dst.val = fcw; |
| 1069 | |
| 1070 | return X86EMUL_CONTINUE; |
| 1071 | } |
| 1072 | |
| 1073 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1074 | { |
| 1075 | u16 fsw; |
| 1076 | |
| 1077 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1078 | return emulate_nm(ctxt); |
| 1079 | |
| 1080 | ctxt->ops->get_fpu(ctxt); |
| 1081 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1082 | ctxt->ops->put_fpu(ctxt); |
| 1083 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1084 | ctxt->dst.val = fsw; |
| 1085 | |
| 1086 | return X86EMUL_CONTINUE; |
| 1087 | } |
| 1088 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1089 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1090 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1091 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1092 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1093 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1094 | if (!(ctxt->d & ModRM)) |
| 1095 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1096 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1097 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1098 | op->type = OP_XMM; |
| 1099 | op->bytes = 16; |
| 1100 | op->addr.xmm = reg; |
| 1101 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1102 | return; |
| 1103 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1104 | if (ctxt->d & Mmx) { |
| 1105 | reg &= 7; |
| 1106 | op->type = OP_MM; |
| 1107 | op->bytes = 8; |
| 1108 | op->addr.mm = reg; |
| 1109 | return; |
| 1110 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1111 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1112 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1113 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1114 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1115 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1116 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1117 | op->orig_val = op->val; |
| 1118 | } |
| 1119 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1120 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1121 | { |
| 1122 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1123 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1124 | } |
| 1125 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1126 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1127 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1128 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1129 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1130 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1131 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1132 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1133 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1134 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1135 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1136 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1137 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1138 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1139 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1140 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1141 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1142 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1143 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1144 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1145 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1146 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1147 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1148 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1149 | op->type = OP_XMM; |
| 1150 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1151 | op->addr.xmm = ctxt->modrm_rm; |
| 1152 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1153 | return rc; |
| 1154 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1155 | if (ctxt->d & Mmx) { |
| 1156 | op->type = OP_MM; |
| 1157 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1158 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1159 | return rc; |
| 1160 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1161 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1162 | return rc; |
| 1163 | } |
| 1164 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1165 | op->type = OP_MEM; |
| 1166 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1167 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1168 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1169 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1170 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1171 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1174 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1175 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1176 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1177 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1178 | break; |
| 1179 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1180 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1181 | break; |
| 1182 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1183 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1184 | break; |
| 1185 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1186 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1187 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1188 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1189 | break; |
| 1190 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1191 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1192 | break; |
| 1193 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1194 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1195 | break; |
| 1196 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1197 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1198 | break; |
| 1199 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1200 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1201 | break; |
| 1202 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1203 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1204 | break; |
| 1205 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1206 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1207 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1208 | break; |
| 1209 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1210 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1211 | break; |
| 1212 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1213 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1214 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1215 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1216 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1217 | } else { |
| 1218 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1219 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1220 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1221 | index_reg |= (sib >> 3) & 7; |
| 1222 | base_reg |= sib & 7; |
| 1223 | scale = sib >> 6; |
| 1224 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1225 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1226 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1227 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1228 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1229 | adjust_modrm_seg(ctxt, base_reg); |
| 1230 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1231 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1232 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1233 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Nadav Amit | 5b38ab8 | 2014-11-02 11:54:41 +0200 | [diff] [blame] | 1234 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1235 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1236 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1237 | } else { |
| 1238 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1239 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1240 | adjust_modrm_seg(ctxt, base_reg); |
| 1241 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1242 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1243 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1244 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1245 | break; |
| 1246 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1247 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1248 | break; |
| 1249 | } |
| 1250 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1251 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1252 | if (ctxt->ad_bytes != 8) |
| 1253 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1254 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1255 | done: |
| 1256 | return rc; |
| 1257 | } |
| 1258 | |
| 1259 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1260 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1261 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1262 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1263 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1264 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1265 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1266 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1267 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1268 | break; |
| 1269 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1270 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1271 | break; |
| 1272 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1273 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | done: |
| 1277 | return rc; |
| 1278 | } |
| 1279 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1280 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1281 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1282 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1283 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1284 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1285 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1286 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1287 | if (ctxt->src.bytes == 2) |
| 1288 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1289 | else if (ctxt->src.bytes == 4) |
| 1290 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1291 | else |
| 1292 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1293 | |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 1294 | ctxt->dst.addr.mem.ea = address_mask(ctxt, |
| 1295 | ctxt->dst.addr.mem.ea + (sv >> 3)); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1296 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1297 | |
| 1298 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1299 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1302 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1303 | unsigned long addr, void *dest, unsigned size) |
| 1304 | { |
| 1305 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1306 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1307 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1308 | if (mc->pos < mc->end) |
| 1309 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1310 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1311 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
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 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1314 | &ctxt->exception); |
| 1315 | if (rc != X86EMUL_CONTINUE) |
| 1316 | return rc; |
| 1317 | |
| 1318 | mc->end += size; |
| 1319 | |
| 1320 | read_cached: |
| 1321 | memcpy(dest, mc->data + mc->pos, size); |
| 1322 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1323 | return X86EMUL_CONTINUE; |
| 1324 | } |
| 1325 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1326 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1327 | struct segmented_address addr, |
| 1328 | void *data, |
| 1329 | unsigned size) |
| 1330 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1331 | int rc; |
| 1332 | ulong linear; |
| 1333 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1334 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1335 | if (rc != X86EMUL_CONTINUE) |
| 1336 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1337 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1341 | struct segmented_address addr, |
| 1342 | const void *data, |
| 1343 | unsigned size) |
| 1344 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1345 | int rc; |
| 1346 | ulong linear; |
| 1347 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1348 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1349 | if (rc != X86EMUL_CONTINUE) |
| 1350 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1351 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1352 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1356 | struct segmented_address addr, |
| 1357 | const void *orig_data, const void *data, |
| 1358 | unsigned size) |
| 1359 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1360 | int rc; |
| 1361 | ulong linear; |
| 1362 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1363 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1364 | if (rc != X86EMUL_CONTINUE) |
| 1365 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1366 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1367 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1370 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1371 | unsigned int size, unsigned short port, |
| 1372 | void *dest) |
| 1373 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1374 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1375 | |
| 1376 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1377 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1378 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1379 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1380 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1381 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1382 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Mark Rustad | b55a814 | 2014-07-25 06:27:05 -0700 | [diff] [blame] | 1383 | n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1384 | if (n == 0) |
| 1385 | n = 1; |
| 1386 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1387 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1388 | return 0; |
| 1389 | rc->end = n * size; |
| 1390 | } |
| 1391 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1392 | if (ctxt->rep_prefix && (ctxt->d & String) && |
| 1393 | !(ctxt->eflags & EFLG_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1394 | ctxt->dst.data = rc->data + rc->pos; |
| 1395 | ctxt->dst.type = OP_MEM_STR; |
| 1396 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1397 | rc->pos = rc->end; |
| 1398 | } else { |
| 1399 | memcpy(dest, rc->data + rc->pos, size); |
| 1400 | rc->pos += size; |
| 1401 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1402 | return 1; |
| 1403 | } |
| 1404 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1405 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1406 | u16 index, struct desc_struct *desc) |
| 1407 | { |
| 1408 | struct desc_ptr dt; |
| 1409 | ulong addr; |
| 1410 | |
| 1411 | ctxt->ops->get_idt(ctxt, &dt); |
| 1412 | |
| 1413 | if (dt.size < index * 8 + 7) |
| 1414 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1415 | |
| 1416 | addr = dt.address + index * 8; |
| 1417 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1418 | &ctxt->exception); |
| 1419 | } |
| 1420 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1421 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1422 | u16 selector, struct desc_ptr *dt) |
| 1423 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1424 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1425 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1426 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1427 | if (selector & 1 << 2) { |
| 1428 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1429 | u16 sel; |
| 1430 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1431 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1432 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1433 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1434 | return; |
| 1435 | |
| 1436 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1437 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1438 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1439 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* allowed just for 8 bytes segments */ |
| 1443 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1444 | u16 selector, struct desc_struct *desc, |
| 1445 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1446 | { |
| 1447 | struct desc_ptr dt; |
| 1448 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1449 | ulong addr; |
| 1450 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1451 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1452 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1453 | if (dt.size < index * 8 + 7) |
| 1454 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1455 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1456 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1457 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1458 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | /* allowed just for 8 bytes segments */ |
| 1462 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1463 | u16 selector, struct desc_struct *desc) |
| 1464 | { |
| 1465 | struct desc_ptr dt; |
| 1466 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1467 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1468 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1469 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1470 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1471 | if (dt.size < index * 8 + 7) |
| 1472 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1473 | |
| 1474 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1475 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1476 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1479 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1480 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1481 | u16 selector, int seg, u8 cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1482 | enum x86_transfer_type transfer, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1483 | struct desc_struct *desc) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1484 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1485 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1486 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1487 | unsigned err_vec = GP_VECTOR; |
| 1488 | u32 err_code = 0; |
| 1489 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1490 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1491 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1492 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1493 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1494 | |
| 1495 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1496 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1497 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1498 | /* set real mode segment descriptor (keep limit etc. for |
| 1499 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1500 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1501 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1502 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1503 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1504 | /* VM86 needs a clean new segment descriptor */ |
| 1505 | set_desc_base(&seg_desc, selector << 4); |
| 1506 | set_desc_limit(&seg_desc, 0xffff); |
| 1507 | seg_desc.type = 3; |
| 1508 | seg_desc.p = 1; |
| 1509 | seg_desc.s = 1; |
| 1510 | seg_desc.dpl = 3; |
| 1511 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1514 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1515 | |
| 1516 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1517 | if ((seg == VCPU_SREG_CS |
| 1518 | || (seg == VCPU_SREG_SS |
| 1519 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1520 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1521 | && null_selector) |
| 1522 | goto exception; |
| 1523 | |
| 1524 | /* TR should be in GDT only */ |
| 1525 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1526 | goto exception; |
| 1527 | |
| 1528 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1529 | goto load; |
| 1530 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1531 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1532 | if (ret != X86EMUL_CONTINUE) |
| 1533 | return ret; |
| 1534 | |
| 1535 | err_code = selector & 0xfffc; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1536 | err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR : |
| 1537 | GP_VECTOR; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1538 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1539 | /* can't load system descriptor into segment selector */ |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1540 | if (seg <= VCPU_SREG_GS && !seg_desc.s) { |
| 1541 | if (transfer == X86_TRANSFER_CALL_JMP) |
| 1542 | return X86EMUL_UNHANDLEABLE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1543 | goto exception; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1544 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1545 | |
| 1546 | if (!seg_desc.p) { |
| 1547 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1548 | goto exception; |
| 1549 | } |
| 1550 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1551 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1552 | |
| 1553 | switch (seg) { |
| 1554 | case VCPU_SREG_SS: |
| 1555 | /* |
| 1556 | * segment is not a writable data segment or segment |
| 1557 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1558 | */ |
| 1559 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1560 | goto exception; |
| 1561 | break; |
| 1562 | case VCPU_SREG_CS: |
| 1563 | if (!(seg_desc.type & 8)) |
| 1564 | goto exception; |
| 1565 | |
| 1566 | if (seg_desc.type & 4) { |
| 1567 | /* conforming */ |
| 1568 | if (dpl > cpl) |
| 1569 | goto exception; |
| 1570 | } else { |
| 1571 | /* nonconforming */ |
| 1572 | if (rpl > cpl || dpl != cpl) |
| 1573 | goto exception; |
| 1574 | } |
Nadav Amit | 040c8dc | 2014-09-18 22:39:43 +0300 | [diff] [blame] | 1575 | /* in long-mode d/b must be clear if l is set */ |
| 1576 | if (seg_desc.d && seg_desc.l) { |
| 1577 | u64 efer = 0; |
| 1578 | |
| 1579 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1580 | if (efer & EFER_LMA) |
| 1581 | goto exception; |
| 1582 | } |
| 1583 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1584 | /* CS(RPL) <- CPL */ |
| 1585 | selector = (selector & 0xfffc) | cpl; |
| 1586 | break; |
| 1587 | case VCPU_SREG_TR: |
| 1588 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1589 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1590 | old_desc = seg_desc; |
| 1591 | seg_desc.type |= 2; /* busy */ |
| 1592 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1593 | sizeof(seg_desc), &ctxt->exception); |
| 1594 | if (ret != X86EMUL_CONTINUE) |
| 1595 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1596 | break; |
| 1597 | case VCPU_SREG_LDTR: |
| 1598 | if (seg_desc.s || seg_desc.type != 2) |
| 1599 | goto exception; |
| 1600 | break; |
| 1601 | default: /* DS, ES, FS, or GS */ |
| 1602 | /* |
| 1603 | * segment is not a data or readable code segment or |
| 1604 | * ((segment is a data or nonconforming code segment) |
| 1605 | * and (both RPL and CPL > DPL)) |
| 1606 | */ |
| 1607 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1608 | (((seg_desc.type & 0xc) != 0xc) && |
| 1609 | (rpl > dpl && cpl > dpl))) |
| 1610 | goto exception; |
| 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | if (seg_desc.s) { |
| 1615 | /* mark segment as accessed */ |
| 1616 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1617 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1618 | if (ret != X86EMUL_CONTINUE) |
| 1619 | return ret; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1620 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1621 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1622 | sizeof(base3), &ctxt->exception); |
| 1623 | if (ret != X86EMUL_CONTINUE) |
| 1624 | return ret; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 1625 | if (is_noncanonical_address(get_desc_base(&seg_desc) | |
| 1626 | ((u64)base3 << 32))) |
| 1627 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1628 | } |
| 1629 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1630 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1631 | if (desc) |
| 1632 | *desc = seg_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1633 | return X86EMUL_CONTINUE; |
| 1634 | exception: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 1635 | return emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1638 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1639 | u16 selector, int seg) |
| 1640 | { |
| 1641 | u8 cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 1642 | return __load_segment_descriptor(ctxt, selector, seg, cpl, |
| 1643 | X86_TRANSFER_NONE, NULL); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1646 | static void write_register_operand(struct operand *op) |
| 1647 | { |
| 1648 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1649 | switch (op->bytes) { |
| 1650 | case 1: |
| 1651 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1652 | break; |
| 1653 | case 2: |
| 1654 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1655 | break; |
| 1656 | case 4: |
| 1657 | *op->addr.reg = (u32)op->val; |
| 1658 | break; /* 64b: zero-extend */ |
| 1659 | case 8: |
| 1660 | *op->addr.reg = op->val; |
| 1661 | break; |
| 1662 | } |
| 1663 | } |
| 1664 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1665 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1666 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1667 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1668 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1669 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1670 | break; |
| 1671 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1672 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1673 | return segmented_cmpxchg(ctxt, |
| 1674 | op->addr.mem, |
| 1675 | &op->orig_val, |
| 1676 | &op->val, |
| 1677 | op->bytes); |
| 1678 | else |
| 1679 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1680 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1681 | &op->val, |
| 1682 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1683 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1684 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1685 | return segmented_write(ctxt, |
| 1686 | op->addr.mem, |
| 1687 | op->data, |
| 1688 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1689 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1690 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1691 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1692 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1693 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1694 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1695 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1696 | case OP_NONE: |
| 1697 | /* no writeback */ |
| 1698 | break; |
| 1699 | default: |
| 1700 | break; |
| 1701 | } |
| 1702 | return X86EMUL_CONTINUE; |
| 1703 | } |
| 1704 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1705 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1706 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1707 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1708 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1709 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1710 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1711 | addr.seg = VCPU_SREG_SS; |
| 1712 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1713 | return segmented_write(ctxt, addr, data, bytes); |
| 1714 | } |
| 1715 | |
| 1716 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1717 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1718 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1719 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1720 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1723 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1724 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1725 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1726 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1727 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1728 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1729 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1730 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1731 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1732 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1733 | return rc; |
| 1734 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1735 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1736 | return rc; |
| 1737 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1738 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1739 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1740 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1741 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1742 | } |
| 1743 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1744 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1745 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1746 | { |
| 1747 | int rc; |
| 1748 | unsigned long val, change_mask; |
| 1749 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1750 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1751 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1752 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1753 | if (rc != X86EMUL_CONTINUE) |
| 1754 | return rc; |
| 1755 | |
| 1756 | 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] | 1757 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1758 | |
| 1759 | switch(ctxt->mode) { |
| 1760 | case X86EMUL_MODE_PROT64: |
| 1761 | case X86EMUL_MODE_PROT32: |
| 1762 | case X86EMUL_MODE_PROT16: |
| 1763 | if (cpl == 0) |
| 1764 | change_mask |= EFLG_IOPL; |
| 1765 | if (cpl <= iopl) |
| 1766 | change_mask |= EFLG_IF; |
| 1767 | break; |
| 1768 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1769 | if (iopl < 3) |
| 1770 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1771 | change_mask |= EFLG_IF; |
| 1772 | break; |
| 1773 | default: /* real mode */ |
| 1774 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1775 | break; |
| 1776 | } |
| 1777 | |
| 1778 | *(unsigned long *)dest = |
| 1779 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1780 | |
| 1781 | return rc; |
| 1782 | } |
| 1783 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1784 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1785 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1786 | ctxt->dst.type = OP_REG; |
| 1787 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1788 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1789 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1790 | } |
| 1791 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1792 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1793 | { |
| 1794 | int rc; |
| 1795 | unsigned frame_size = ctxt->src.val; |
| 1796 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1797 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1798 | |
| 1799 | if (nesting_level) |
| 1800 | return X86EMUL_UNHANDLEABLE; |
| 1801 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1802 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1803 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1804 | if (rc != X86EMUL_CONTINUE) |
| 1805 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1806 | 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] | 1807 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1808 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1809 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1810 | stack_mask(ctxt)); |
| 1811 | return X86EMUL_CONTINUE; |
| 1812 | } |
| 1813 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1814 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1815 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1816 | 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] | 1817 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1818 | 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] | 1819 | } |
| 1820 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1821 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1822 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1823 | int seg = ctxt->src2.val; |
| 1824 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1825 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Nadav Amit | 0fcc207 | 2014-11-02 11:54:51 +0200 | [diff] [blame] | 1826 | if (ctxt->op_bytes == 4) { |
| 1827 | rsp_increment(ctxt, -2); |
| 1828 | ctxt->op_bytes = 2; |
| 1829 | } |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1830 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1831 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1832 | } |
| 1833 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1834 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1835 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1836 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1837 | unsigned long selector; |
| 1838 | int rc; |
| 1839 | |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1840 | rc = emulate_pop(ctxt, &selector, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1841 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1842 | return rc; |
| 1843 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1844 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1845 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Nadav Amit | 3313bc4 | 2014-12-25 02:52:17 +0200 | [diff] [blame] | 1846 | if (ctxt->op_bytes > 2) |
| 1847 | rsp_increment(ctxt, ctxt->op_bytes - 2); |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1848 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1849 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1850 | return rc; |
| 1851 | } |
| 1852 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1853 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1854 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1855 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1856 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1857 | int reg = VCPU_REGS_RAX; |
| 1858 | |
| 1859 | while (reg <= VCPU_REGS_RDI) { |
| 1860 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1861 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1862 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1863 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1864 | if (rc != X86EMUL_CONTINUE) |
| 1865 | return rc; |
| 1866 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1867 | ++reg; |
| 1868 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1869 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1870 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1873 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1874 | { |
Nadav Amit | bc397a6 | 2014-12-10 11:19:03 +0200 | [diff] [blame] | 1875 | ctxt->src.val = (unsigned long)ctxt->eflags & ~EFLG_VM; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1876 | return em_push(ctxt); |
| 1877 | } |
| 1878 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1879 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1880 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1881 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1882 | int reg = VCPU_REGS_RDI; |
| 1883 | |
| 1884 | while (reg >= VCPU_REGS_RAX) { |
| 1885 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1886 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1887 | --reg; |
| 1888 | } |
| 1889 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1890 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1891 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1892 | break; |
| 1893 | --reg; |
| 1894 | } |
| 1895 | return rc; |
| 1896 | } |
| 1897 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1898 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1899 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1900 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1901 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1902 | struct desc_ptr dt; |
| 1903 | gva_t cs_addr; |
| 1904 | gva_t eip_addr; |
| 1905 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1906 | |
| 1907 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1908 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1909 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1910 | if (rc != X86EMUL_CONTINUE) |
| 1911 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1912 | |
| 1913 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1914 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1915 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1916 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1917 | if (rc != X86EMUL_CONTINUE) |
| 1918 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1919 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1920 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1921 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1922 | if (rc != X86EMUL_CONTINUE) |
| 1923 | return rc; |
| 1924 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1925 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1926 | |
| 1927 | eip_addr = dt.address + (irq << 2); |
| 1928 | cs_addr = dt.address + (irq << 2) + 2; |
| 1929 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1930 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1931 | if (rc != X86EMUL_CONTINUE) |
| 1932 | return rc; |
| 1933 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1934 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1935 | if (rc != X86EMUL_CONTINUE) |
| 1936 | return rc; |
| 1937 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1938 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1939 | if (rc != X86EMUL_CONTINUE) |
| 1940 | return rc; |
| 1941 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1942 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1943 | |
| 1944 | return rc; |
| 1945 | } |
| 1946 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1947 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1948 | { |
| 1949 | int rc; |
| 1950 | |
| 1951 | invalidate_registers(ctxt); |
| 1952 | rc = __emulate_int_real(ctxt, irq); |
| 1953 | if (rc == X86EMUL_CONTINUE) |
| 1954 | writeback_registers(ctxt); |
| 1955 | return rc; |
| 1956 | } |
| 1957 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1958 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1959 | { |
| 1960 | switch(ctxt->mode) { |
| 1961 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1962 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1963 | case X86EMUL_MODE_VM86: |
| 1964 | case X86EMUL_MODE_PROT16: |
| 1965 | case X86EMUL_MODE_PROT32: |
| 1966 | case X86EMUL_MODE_PROT64: |
| 1967 | default: |
| 1968 | /* Protected mode interrupts unimplemented yet */ |
| 1969 | return X86EMUL_UNHANDLEABLE; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1973 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1974 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1975 | int rc = X86EMUL_CONTINUE; |
| 1976 | unsigned long temp_eip = 0; |
| 1977 | unsigned long temp_eflags = 0; |
| 1978 | unsigned long cs = 0; |
| 1979 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1980 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1981 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1982 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1983 | |
| 1984 | /* TODO: Add stack limit check */ |
| 1985 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1986 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1987 | |
| 1988 | if (rc != X86EMUL_CONTINUE) |
| 1989 | return rc; |
| 1990 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1991 | if (temp_eip & ~0xffff) |
| 1992 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1993 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1994 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1995 | |
| 1996 | if (rc != X86EMUL_CONTINUE) |
| 1997 | return rc; |
| 1998 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1999 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2000 | |
| 2001 | if (rc != X86EMUL_CONTINUE) |
| 2002 | return rc; |
| 2003 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2004 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2005 | |
| 2006 | if (rc != X86EMUL_CONTINUE) |
| 2007 | return rc; |
| 2008 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2009 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2010 | |
| 2011 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2012 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2013 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2014 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2015 | ctxt->eflags &= ~0xffff; |
| 2016 | ctxt->eflags |= temp_eflags; |
| 2017 | } |
| 2018 | |
| 2019 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 2020 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 2021 | |
| 2022 | return rc; |
| 2023 | } |
| 2024 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2025 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2026 | { |
| 2027 | switch(ctxt->mode) { |
| 2028 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2029 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2030 | case X86EMUL_MODE_VM86: |
| 2031 | case X86EMUL_MODE_PROT16: |
| 2032 | case X86EMUL_MODE_PROT32: |
| 2033 | case X86EMUL_MODE_PROT64: |
| 2034 | default: |
| 2035 | /* iret from protected mode unimplemented yet */ |
| 2036 | return X86EMUL_UNHANDLEABLE; |
| 2037 | } |
| 2038 | } |
| 2039 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2040 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2041 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2042 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2043 | unsigned short sel, old_sel; |
| 2044 | struct desc_struct old_desc, new_desc; |
| 2045 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 2046 | u8 cpl = ctxt->ops->cpl(ctxt); |
| 2047 | |
| 2048 | /* Assignment of RIP may only fail in 64-bit mode */ |
| 2049 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2050 | ops->get_segment(ctxt, &old_sel, &old_desc, NULL, |
| 2051 | VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2052 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2053 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2054 | |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2055 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 2056 | X86_TRANSFER_CALL_JMP, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2057 | &new_desc); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2058 | if (rc != X86EMUL_CONTINUE) |
| 2059 | return rc; |
| 2060 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2061 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2062 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2063 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2064 | /* assigning eip failed; restore the old cs */ |
| 2065 | ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS); |
| 2066 | return rc; |
| 2067 | } |
| 2068 | return rc; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2069 | } |
| 2070 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2071 | static int em_jmp_abs(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2072 | { |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2073 | return assign_eip_near(ctxt, ctxt->src.val); |
| 2074 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2075 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2076 | static int em_call_near_abs(struct x86_emulate_ctxt *ctxt) |
| 2077 | { |
| 2078 | int rc; |
| 2079 | long int old_eip; |
| 2080 | |
| 2081 | old_eip = ctxt->_eip; |
| 2082 | rc = assign_eip_near(ctxt, ctxt->src.val); |
| 2083 | if (rc != X86EMUL_CONTINUE) |
| 2084 | return rc; |
| 2085 | ctxt->src.val = old_eip; |
| 2086 | rc = em_push(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2087 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2090 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2091 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2092 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2093 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2094 | if (ctxt->dst.bytes == 16) |
| 2095 | return X86EMUL_UNHANDLEABLE; |
| 2096 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2097 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2098 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2099 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2100 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2101 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2102 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2103 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2104 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2105 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2106 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2107 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2108 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2111 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2112 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2113 | int rc; |
| 2114 | unsigned long eip; |
| 2115 | |
| 2116 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 2117 | if (rc != X86EMUL_CONTINUE) |
| 2118 | return rc; |
| 2119 | |
| 2120 | return assign_eip_near(ctxt, eip); |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2121 | } |
| 2122 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2123 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2124 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2125 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2126 | unsigned long eip, cs; |
| 2127 | u16 old_cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2128 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2129 | struct desc_struct old_desc, new_desc; |
| 2130 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2131 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2132 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2133 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, |
| 2134 | VCPU_SREG_CS); |
| 2135 | |
| 2136 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2137 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2138 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2139 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2140 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2141 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2142 | /* Outer-privilege level return is not implemented */ |
| 2143 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2144 | return X86EMUL_UNHANDLEABLE; |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2145 | rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl, |
| 2146 | X86_TRANSFER_RET, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2147 | &new_desc); |
| 2148 | if (rc != X86EMUL_CONTINUE) |
| 2149 | return rc; |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 2150 | rc = assign_eip_far(ctxt, eip, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2151 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | 7e46ddd | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2152 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2153 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 2154 | } |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2155 | return rc; |
| 2156 | } |
| 2157 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2158 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2159 | { |
| 2160 | int rc; |
| 2161 | |
| 2162 | rc = em_ret_far(ctxt); |
| 2163 | if (rc != X86EMUL_CONTINUE) |
| 2164 | return rc; |
| 2165 | rsp_increment(ctxt, ctxt->src.val); |
| 2166 | return X86EMUL_CONTINUE; |
| 2167 | } |
| 2168 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2169 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2170 | { |
| 2171 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2172 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2173 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2174 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2175 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2176 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2177 | |
| 2178 | if (ctxt->eflags & EFLG_ZF) { |
| 2179 | /* Success: write back to memory. */ |
| 2180 | ctxt->dst.val = ctxt->src.orig_val; |
| 2181 | } else { |
| 2182 | /* Failure: write the value we saw to EAX. */ |
| 2183 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2184 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2185 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2186 | } |
| 2187 | return X86EMUL_CONTINUE; |
| 2188 | } |
| 2189 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2190 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2191 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2192 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2193 | unsigned short sel; |
| 2194 | int rc; |
| 2195 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2196 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2197 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2198 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2199 | if (rc != X86EMUL_CONTINUE) |
| 2200 | return rc; |
| 2201 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2202 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2203 | return rc; |
| 2204 | } |
| 2205 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2206 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2207 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2208 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2209 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2210 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2211 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2212 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2213 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2214 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2215 | cs->s = 1; |
| 2216 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2217 | cs->p = 1; |
| 2218 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2219 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2220 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2221 | set_desc_base(ss, 0); /* flat segment */ |
| 2222 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2223 | ss->g = 1; /* 4kb granularity */ |
| 2224 | ss->s = 1; |
| 2225 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2226 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2227 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2228 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2229 | ss->l = 0; |
| 2230 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2231 | } |
| 2232 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2233 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2234 | { |
| 2235 | u32 eax, ebx, ecx, edx; |
| 2236 | |
| 2237 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2238 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2239 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2240 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2241 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2242 | } |
| 2243 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2244 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2245 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2246 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2247 | u32 eax, ebx, ecx, edx; |
| 2248 | |
| 2249 | /* |
| 2250 | * syscall should always be enabled in longmode - so only become |
| 2251 | * vendor specific (cpuid) if other modes are active... |
| 2252 | */ |
| 2253 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2254 | return true; |
| 2255 | |
| 2256 | eax = 0x00000000; |
| 2257 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2258 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2259 | /* |
| 2260 | * Intel ("GenuineIntel") |
| 2261 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2262 | * longmode. Also an 64bit guest with a |
| 2263 | * 32bit compat-app running will #UD !! While this |
| 2264 | * behaviour can be fixed (by emulating) into AMD |
| 2265 | * response - CPUs of AMD can't behave like Intel. |
| 2266 | */ |
| 2267 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2268 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2269 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2270 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2271 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2272 | /* AMD ("AuthenticAMD") */ |
| 2273 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2274 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2275 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2276 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2277 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2278 | /* AMD ("AMDisbetter!") */ |
| 2279 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2280 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2281 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2282 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2283 | |
| 2284 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2285 | return false; |
| 2286 | } |
| 2287 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2288 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2289 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2290 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2291 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2292 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2293 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2294 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2295 | |
| 2296 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2297 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2298 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2299 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2300 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2301 | if (!(em_syscall_is_enabled(ctxt))) |
| 2302 | return emulate_ud(ctxt); |
| 2303 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2304 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2305 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2306 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2307 | if (!(efer & EFER_SCE)) |
| 2308 | return emulate_ud(ctxt); |
| 2309 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2310 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2311 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2312 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2313 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2314 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2315 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2316 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2317 | cs.l = 1; |
| 2318 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2319 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2320 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2321 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2322 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2323 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2324 | #ifdef CONFIG_X86_64 |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2325 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2326 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2327 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2328 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2329 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2330 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2331 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2332 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2333 | ctxt->eflags &= ~msr_data; |
Nadav Amit | 807c142 | 2014-11-02 11:54:49 +0200 | [diff] [blame] | 2334 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2335 | #endif |
| 2336 | } else { |
| 2337 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2338 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2339 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2340 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2341 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2344 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2345 | } |
| 2346 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2347 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2348 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2349 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2350 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2351 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2352 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2353 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2354 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2355 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2356 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2357 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2358 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2359 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2360 | /* |
| 2361 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2362 | * mode). |
| 2363 | */ |
| 2364 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2365 | && !vendor_intel(ctxt)) |
| 2366 | return emulate_ud(ctxt); |
| 2367 | |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2368 | /* sysenter/sysexit have not been tested in 64bit mode. */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2369 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Nadav Amit | b2c9d43 | 2014-11-02 11:55:01 +0200 | [diff] [blame] | 2370 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2371 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2372 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2373 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2374 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2375 | switch (ctxt->mode) { |
| 2376 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2377 | if ((msr_data & 0xfffc) == 0x0) |
| 2378 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2379 | break; |
| 2380 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2381 | if (msr_data == 0x0) |
| 2382 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2383 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2384 | default: |
| 2385 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2386 | } |
| 2387 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2388 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2389 | cs_sel = (u16)msr_data; |
| 2390 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2391 | ss_sel = cs_sel + 8; |
| 2392 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2393 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2394 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2395 | cs.l = 1; |
| 2396 | } |
| 2397 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2398 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2399 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2400 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2401 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2402 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2403 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2404 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2405 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2406 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2407 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2410 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2411 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2412 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2413 | struct desc_struct cs, ss; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2414 | u64 msr_data, rcx, rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2415 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2416 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2417 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2418 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2419 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2420 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2421 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2422 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2423 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2424 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2425 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2426 | usermode = X86EMUL_MODE_PROT64; |
| 2427 | else |
| 2428 | usermode = X86EMUL_MODE_PROT32; |
| 2429 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2430 | rcx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2431 | rdx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2432 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2433 | cs.dpl = 3; |
| 2434 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2435 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2436 | switch (usermode) { |
| 2437 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2438 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2439 | if ((msr_data & 0xfffc) == 0x0) |
| 2440 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2441 | ss_sel = (u16)(msr_data + 24); |
Nadav Amit | bf0b682 | 2014-09-18 22:39:45 +0300 | [diff] [blame] | 2442 | rcx = (u32)rcx; |
| 2443 | rdx = (u32)rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2444 | break; |
| 2445 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2446 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2447 | if (msr_data == 0x0) |
| 2448 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2449 | ss_sel = cs_sel + 8; |
| 2450 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2451 | cs.l = 1; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2452 | if (is_noncanonical_address(rcx) || |
| 2453 | is_noncanonical_address(rdx)) |
| 2454 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2455 | break; |
| 2456 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2457 | cs_sel |= SELECTOR_RPL_MASK; |
| 2458 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2459 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2460 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2461 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2462 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2463 | ctxt->_eip = rdx; |
| 2464 | *reg_write(ctxt, VCPU_REGS_RSP) = rcx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2465 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2466 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2469 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2470 | { |
| 2471 | int iopl; |
| 2472 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2473 | return false; |
| 2474 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2475 | return true; |
| 2476 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2477 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2481 | u16 port, u16 len) |
| 2482 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2483 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2484 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2485 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2486 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2487 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2488 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2489 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2490 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2491 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2492 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2493 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2494 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2495 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2496 | base = get_desc_base(&tr_seg); |
| 2497 | #ifdef CONFIG_X86_64 |
| 2498 | base |= ((u64)base3) << 32; |
| 2499 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2500 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2501 | if (r != X86EMUL_CONTINUE) |
| 2502 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2503 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2504 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2505 | 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] | 2506 | if (r != X86EMUL_CONTINUE) |
| 2507 | return false; |
| 2508 | if ((perm >> bit_idx) & mask) |
| 2509 | return false; |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
| 2513 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2514 | u16 port, u16 len) |
| 2515 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2516 | if (ctxt->perm_ok) |
| 2517 | return true; |
| 2518 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2519 | if (emulator_bad_iopl(ctxt)) |
| 2520 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2521 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2522 | |
| 2523 | ctxt->perm_ok = true; |
| 2524 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2525 | return true; |
| 2526 | } |
| 2527 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2528 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2529 | struct tss_segment_16 *tss) |
| 2530 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2531 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2532 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2533 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2534 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2535 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2536 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2537 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2538 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2539 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2540 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2541 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2542 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2543 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2544 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2545 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2546 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2550 | struct tss_segment_16 *tss) |
| 2551 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2552 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2553 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2554 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2555 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2556 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2557 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2558 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2559 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2560 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2561 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2562 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2563 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2564 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2565 | |
| 2566 | /* |
| 2567 | * SDM says that segment selectors are loaded before segment |
| 2568 | * descriptors |
| 2569 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2570 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2571 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2572 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2573 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2574 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2575 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2576 | cpl = tss->cs & 3; |
| 2577 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2578 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2579 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2580 | * it is handled in a context of new task |
| 2581 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2582 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2583 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2584 | if (ret != X86EMUL_CONTINUE) |
| 2585 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2586 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2587 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2588 | if (ret != X86EMUL_CONTINUE) |
| 2589 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2590 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2591 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2592 | if (ret != X86EMUL_CONTINUE) |
| 2593 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2594 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2595 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2596 | if (ret != X86EMUL_CONTINUE) |
| 2597 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2598 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2599 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2600 | if (ret != X86EMUL_CONTINUE) |
| 2601 | return ret; |
| 2602 | |
| 2603 | return X86EMUL_CONTINUE; |
| 2604 | } |
| 2605 | |
| 2606 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2607 | u16 tss_selector, u16 old_tss_sel, |
| 2608 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2609 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2610 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2611 | struct tss_segment_16 tss_seg; |
| 2612 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2613 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2614 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2615 | 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] | 2616 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2617 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2618 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2619 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2620 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2621 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2622 | ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2623 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2624 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2625 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2626 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2627 | 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] | 2628 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2629 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2630 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | |
| 2632 | if (old_tss_sel != 0xffff) { |
| 2633 | tss_seg.prev_task_link = old_tss_sel; |
| 2634 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2635 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2636 | &tss_seg.prev_task_link, |
| 2637 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2638 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2639 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2641 | } |
| 2642 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2643 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | struct tss_segment_32 *tss) |
| 2648 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2649 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2650 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2651 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2652 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2653 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2654 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2655 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2656 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2657 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2658 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2659 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2660 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2661 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2662 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2663 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2664 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2665 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2666 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2670 | struct tss_segment_32 *tss) |
| 2671 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2672 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2673 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2674 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2675 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2676 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2677 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2678 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2679 | |
| 2680 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2681 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2682 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2683 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2684 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2685 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2686 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2687 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2688 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2689 | |
| 2690 | /* |
| 2691 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2692 | * descriptors. This is important because CPL checks will |
| 2693 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2694 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2695 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2696 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2697 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2698 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2699 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2700 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2701 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2702 | |
| 2703 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2704 | * If we're switching between Protected Mode and VM86, we need to make |
| 2705 | * sure to update the mode before loading the segment descriptors so |
| 2706 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2707 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2708 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2709 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2710 | cpl = 3; |
| 2711 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2712 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2713 | cpl = tss->cs & 3; |
| 2714 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2715 | |
| 2716 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2717 | * Now load segment descriptors. If fault happenes at this stage |
| 2718 | * it is handled in a context of new task |
| 2719 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2720 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2721 | cpl, X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2722 | if (ret != X86EMUL_CONTINUE) |
| 2723 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2724 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2725 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2726 | if (ret != X86EMUL_CONTINUE) |
| 2727 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2728 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2729 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2730 | if (ret != X86EMUL_CONTINUE) |
| 2731 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2732 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2733 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2734 | if (ret != X86EMUL_CONTINUE) |
| 2735 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2736 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2737 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2738 | if (ret != X86EMUL_CONTINUE) |
| 2739 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2740 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2741 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2742 | if (ret != X86EMUL_CONTINUE) |
| 2743 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2744 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 2745 | X86_TRANSFER_TASK_SWITCH, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2746 | if (ret != X86EMUL_CONTINUE) |
| 2747 | return ret; |
| 2748 | |
| 2749 | return X86EMUL_CONTINUE; |
| 2750 | } |
| 2751 | |
| 2752 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2753 | u16 tss_selector, u16 old_tss_sel, |
| 2754 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2755 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2756 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2757 | struct tss_segment_32 tss_seg; |
| 2758 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2759 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2760 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2761 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2762 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2763 | 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] | 2764 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2765 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2766 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2767 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2768 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2769 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2770 | /* Only GP registers and segment selectors are saved */ |
| 2771 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2772 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2773 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2774 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2775 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2776 | 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] | 2777 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2778 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2779 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2780 | |
| 2781 | if (old_tss_sel != 0xffff) { |
| 2782 | tss_seg.prev_task_link = old_tss_sel; |
| 2783 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2784 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2785 | &tss_seg.prev_task_link, |
| 2786 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2787 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2788 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2789 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2792 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2796 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2797 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2798 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2799 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2800 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2801 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2802 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2803 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2804 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2805 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2806 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2807 | |
| 2808 | /* FIXME: old_tss_base == ~0 ? */ |
| 2809 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2810 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2811 | if (ret != X86EMUL_CONTINUE) |
| 2812 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2813 | 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] | 2814 | if (ret != X86EMUL_CONTINUE) |
| 2815 | return ret; |
| 2816 | |
| 2817 | /* FIXME: check that next_tss_desc is tss */ |
| 2818 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2819 | /* |
| 2820 | * Check privileges. The three cases are task switch caused by... |
| 2821 | * |
| 2822 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2823 | * 2. Exception/IRQ/iret: No check is performed |
Nadav Amit | 2c2ca2d | 2014-11-02 11:54:57 +0200 | [diff] [blame] | 2824 | * 3. jmp/call to TSS/task-gate: No check is performed since the |
| 2825 | * hardware checks it before exiting. |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2826 | */ |
| 2827 | if (reason == TASK_SWITCH_GATE) { |
| 2828 | if (idt_index != -1) { |
| 2829 | /* Software interrupts */ |
| 2830 | struct desc_struct task_gate_desc; |
| 2831 | int dpl; |
| 2832 | |
| 2833 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2834 | &task_gate_desc); |
| 2835 | if (ret != X86EMUL_CONTINUE) |
| 2836 | return ret; |
| 2837 | |
| 2838 | dpl = task_gate_desc.dpl; |
| 2839 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2840 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2841 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2844 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2845 | if (!next_tss_desc.p || |
| 2846 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2847 | desc_limit < 0x2b)) { |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 2848 | return emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2852 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2853 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | if (reason == TASK_SWITCH_IRET) |
| 2857 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2858 | |
| 2859 | /* 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] | 2860 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2861 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2862 | old_tss_sel = 0xffff; |
| 2863 | |
| 2864 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2865 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2866 | old_tss_base, &next_tss_desc); |
| 2867 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2868 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2869 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2870 | if (ret != X86EMUL_CONTINUE) |
| 2871 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2872 | |
| 2873 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2874 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2875 | |
| 2876 | if (reason != TASK_SWITCH_IRET) { |
| 2877 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2878 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2881 | 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] | 2882 | 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] | 2883 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2884 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2885 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2886 | ctxt->lock_prefix = 0; |
| 2887 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2888 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2891 | return ret; |
| 2892 | } |
| 2893 | |
| 2894 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2895 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2896 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2897 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2898 | int rc; |
| 2899 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2900 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2901 | ctxt->_eip = ctxt->eip; |
| 2902 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2903 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2904 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2905 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2906 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2907 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2908 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2909 | writeback_registers(ctxt); |
| 2910 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2911 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2912 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2913 | } |
| 2914 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2915 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2916 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2917 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2918 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2919 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 2920 | register_address_increment(ctxt, reg, df * op->bytes); |
| 2921 | op->addr.mem.ea = register_address(ctxt, reg); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2924 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2925 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2926 | u8 al, old_al; |
| 2927 | bool af, cf, old_cf; |
| 2928 | |
| 2929 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2930 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2931 | |
| 2932 | old_al = al; |
| 2933 | old_cf = cf; |
| 2934 | cf = false; |
| 2935 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2936 | if ((al & 0x0f) > 9 || af) { |
| 2937 | al -= 6; |
| 2938 | cf = old_cf | (al >= 250); |
| 2939 | af = true; |
| 2940 | } else { |
| 2941 | af = false; |
| 2942 | } |
| 2943 | if (old_al > 0x99 || old_cf) { |
| 2944 | al -= 0x60; |
| 2945 | cf = true; |
| 2946 | } |
| 2947 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2948 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2949 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2950 | ctxt->src.type = OP_IMM; |
| 2951 | ctxt->src.val = 0; |
| 2952 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2953 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2954 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2955 | if (cf) |
| 2956 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2957 | if (af) |
| 2958 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2959 | return X86EMUL_CONTINUE; |
| 2960 | } |
| 2961 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2962 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2963 | { |
| 2964 | u8 al, ah; |
| 2965 | |
| 2966 | if (ctxt->src.val == 0) |
| 2967 | return emulate_de(ctxt); |
| 2968 | |
| 2969 | al = ctxt->dst.val & 0xff; |
| 2970 | ah = al / ctxt->src.val; |
| 2971 | al %= ctxt->src.val; |
| 2972 | |
| 2973 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 2974 | |
| 2975 | /* Set PF, ZF, SF */ |
| 2976 | ctxt->src.type = OP_IMM; |
| 2977 | ctxt->src.val = 0; |
| 2978 | ctxt->src.bytes = 1; |
| 2979 | fastop(ctxt, em_or); |
| 2980 | |
| 2981 | return X86EMUL_CONTINUE; |
| 2982 | } |
| 2983 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2984 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2985 | { |
| 2986 | u8 al = ctxt->dst.val & 0xff; |
| 2987 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2988 | |
| 2989 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2990 | |
| 2991 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2992 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 2993 | /* Set PF, ZF, SF */ |
| 2994 | ctxt->src.type = OP_IMM; |
| 2995 | ctxt->src.val = 0; |
| 2996 | ctxt->src.bytes = 1; |
| 2997 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2998 | |
| 2999 | return X86EMUL_CONTINUE; |
| 3000 | } |
| 3001 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3002 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3003 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3004 | int rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3005 | long rel = ctxt->src.val; |
| 3006 | |
| 3007 | ctxt->src.val = (unsigned long)ctxt->_eip; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3008 | rc = jmp_rel(ctxt, rel); |
| 3009 | if (rc != X86EMUL_CONTINUE) |
| 3010 | return rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3011 | return em_push(ctxt); |
| 3012 | } |
| 3013 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3014 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3015 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3016 | u16 sel, old_cs; |
| 3017 | ulong old_eip; |
| 3018 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3019 | struct desc_struct old_desc, new_desc; |
| 3020 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 3021 | int cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3022 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3023 | old_eip = ctxt->_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3024 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3025 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3026 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Nadav Amit | 3dc4bc4 | 2014-12-25 02:52:19 +0200 | [diff] [blame] | 3027 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, |
| 3028 | X86_TRANSFER_CALL_JMP, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3029 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | 80976db | 2014-12-25 02:52:20 +0200 | [diff] [blame^] | 3030 | return rc; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3031 | |
Nadav Amit | d50eaa1 | 2014-11-19 17:43:11 +0200 | [diff] [blame] | 3032 | rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3033 | if (rc != X86EMUL_CONTINUE) |
| 3034 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3035 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3036 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3037 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3038 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3039 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3040 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3041 | ctxt->src.val = old_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3042 | rc = em_push(ctxt); |
| 3043 | /* If we failed, we tainted the memory, but the very least we should |
| 3044 | restore cs */ |
| 3045 | if (rc != X86EMUL_CONTINUE) |
| 3046 | goto fail; |
| 3047 | return rc; |
| 3048 | fail: |
| 3049 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 3050 | return rc; |
| 3051 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3052 | } |
| 3053 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3054 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3055 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3056 | int rc; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3057 | unsigned long eip; |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3058 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3059 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 3060 | if (rc != X86EMUL_CONTINUE) |
| 3061 | return rc; |
| 3062 | rc = assign_eip_near(ctxt, eip); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3063 | if (rc != X86EMUL_CONTINUE) |
| 3064 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3065 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3066 | return X86EMUL_CONTINUE; |
| 3067 | } |
| 3068 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3069 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3070 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3071 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3072 | ctxt->src.val = ctxt->dst.val; |
| 3073 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3074 | |
| 3075 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3076 | ctxt->dst.val = ctxt->src.orig_val; |
| 3077 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3078 | return X86EMUL_CONTINUE; |
| 3079 | } |
| 3080 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3081 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3082 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3083 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3084 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3085 | } |
| 3086 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3087 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3088 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3089 | ctxt->dst.type = OP_REG; |
| 3090 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3091 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3092 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3093 | |
| 3094 | return X86EMUL_CONTINUE; |
| 3095 | } |
| 3096 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3097 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3098 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3099 | u64 tsc = 0; |
| 3100 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3101 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3102 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3103 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3104 | return X86EMUL_CONTINUE; |
| 3105 | } |
| 3106 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3107 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3108 | { |
| 3109 | u64 pmc; |
| 3110 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3111 | 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] | 3112 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3113 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3114 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3115 | return X86EMUL_CONTINUE; |
| 3116 | } |
| 3117 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3118 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3119 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 3120 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3121 | return X86EMUL_CONTINUE; |
| 3122 | } |
| 3123 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3124 | #define FFL(x) bit(X86_FEATURE_##x) |
| 3125 | |
| 3126 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 3127 | { |
| 3128 | u32 ebx, ecx, edx, eax = 1; |
| 3129 | u16 tmp; |
| 3130 | |
| 3131 | /* |
| 3132 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 3133 | */ |
| 3134 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3135 | if (!(ecx & FFL(MOVBE))) |
| 3136 | return emulate_ud(ctxt); |
| 3137 | |
| 3138 | switch (ctxt->op_bytes) { |
| 3139 | case 2: |
| 3140 | /* |
| 3141 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3142 | * the upper word of the destination register remains unchanged |
| 3143 | * ..." |
| 3144 | * |
| 3145 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3146 | * rules so we have to do the operation almost per hand. |
| 3147 | */ |
| 3148 | tmp = (u16)ctxt->src.val; |
| 3149 | ctxt->dst.val &= ~0xffffUL; |
| 3150 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3151 | break; |
| 3152 | case 4: |
| 3153 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3154 | break; |
| 3155 | case 8: |
| 3156 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3157 | break; |
| 3158 | default: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3159 | BUG(); |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3160 | } |
| 3161 | return X86EMUL_CONTINUE; |
| 3162 | } |
| 3163 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3164 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3165 | { |
| 3166 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3167 | return emulate_gp(ctxt, 0); |
| 3168 | |
| 3169 | /* Disable writeback. */ |
| 3170 | ctxt->dst.type = OP_NONE; |
| 3171 | return X86EMUL_CONTINUE; |
| 3172 | } |
| 3173 | |
| 3174 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3175 | { |
| 3176 | unsigned long val; |
| 3177 | |
| 3178 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3179 | val = ctxt->src.val & ~0ULL; |
| 3180 | else |
| 3181 | val = ctxt->src.val & ~0U; |
| 3182 | |
| 3183 | /* #UD condition is already handled. */ |
| 3184 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3185 | return emulate_gp(ctxt, 0); |
| 3186 | |
| 3187 | /* Disable writeback. */ |
| 3188 | ctxt->dst.type = OP_NONE; |
| 3189 | return X86EMUL_CONTINUE; |
| 3190 | } |
| 3191 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3192 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3193 | { |
| 3194 | u64 msr_data; |
| 3195 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3196 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3197 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3198 | 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] | 3199 | return emulate_gp(ctxt, 0); |
| 3200 | |
| 3201 | return X86EMUL_CONTINUE; |
| 3202 | } |
| 3203 | |
| 3204 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3205 | { |
| 3206 | u64 msr_data; |
| 3207 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3208 | 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] | 3209 | return emulate_gp(ctxt, 0); |
| 3210 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3211 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3212 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3213 | return X86EMUL_CONTINUE; |
| 3214 | } |
| 3215 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3216 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3217 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3218 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3219 | return emulate_ud(ctxt); |
| 3220 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3221 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Nadav Amit | b5bbf10 | 2014-11-02 11:54:46 +0200 | [diff] [blame] | 3222 | if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM) |
| 3223 | ctxt->dst.bytes = 2; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3224 | return X86EMUL_CONTINUE; |
| 3225 | } |
| 3226 | |
| 3227 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3228 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3229 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3230 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3231 | 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] | 3232 | return emulate_ud(ctxt); |
| 3233 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3234 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3235 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3236 | |
| 3237 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3238 | ctxt->dst.type = OP_NONE; |
| 3239 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3240 | } |
| 3241 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3242 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3243 | { |
| 3244 | u16 sel = ctxt->src.val; |
| 3245 | |
| 3246 | /* Disable writeback. */ |
| 3247 | ctxt->dst.type = OP_NONE; |
| 3248 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3249 | } |
| 3250 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3251 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3252 | { |
| 3253 | u16 sel = ctxt->src.val; |
| 3254 | |
| 3255 | /* Disable writeback. */ |
| 3256 | ctxt->dst.type = OP_NONE; |
| 3257 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3258 | } |
| 3259 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3260 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3261 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3262 | int rc; |
| 3263 | ulong linear; |
| 3264 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3265 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3266 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3267 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3268 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3269 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3270 | return X86EMUL_CONTINUE; |
| 3271 | } |
| 3272 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3273 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3274 | { |
| 3275 | ulong cr0; |
| 3276 | |
| 3277 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3278 | cr0 &= ~X86_CR0_TS; |
| 3279 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3280 | return X86EMUL_CONTINUE; |
| 3281 | } |
| 3282 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3283 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3284 | { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3285 | int rc = ctxt->ops->fix_hypercall(ctxt); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3286 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3287 | if (rc != X86EMUL_CONTINUE) |
| 3288 | return rc; |
| 3289 | |
| 3290 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3291 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3292 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3293 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3294 | return X86EMUL_CONTINUE; |
| 3295 | } |
| 3296 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3297 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3298 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3299 | struct desc_ptr *ptr)) |
| 3300 | { |
| 3301 | struct desc_ptr desc_ptr; |
| 3302 | |
| 3303 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3304 | ctxt->op_bytes = 8; |
| 3305 | get(ctxt, &desc_ptr); |
| 3306 | if (ctxt->op_bytes == 2) { |
| 3307 | ctxt->op_bytes = 4; |
| 3308 | desc_ptr.address &= 0x00ffffff; |
| 3309 | } |
| 3310 | /* Disable writeback. */ |
| 3311 | ctxt->dst.type = OP_NONE; |
| 3312 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3313 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3314 | } |
| 3315 | |
| 3316 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3317 | { |
| 3318 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3319 | } |
| 3320 | |
| 3321 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3322 | { |
| 3323 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3324 | } |
| 3325 | |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3326 | static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3327 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3328 | struct desc_ptr desc_ptr; |
| 3329 | int rc; |
| 3330 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3331 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3332 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3333 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3334 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3335 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3336 | if (rc != X86EMUL_CONTINUE) |
| 3337 | return rc; |
Nadav Amit | 9a9abf6 | 2014-11-02 11:54:56 +0200 | [diff] [blame] | 3338 | if (ctxt->mode == X86EMUL_MODE_PROT64 && |
| 3339 | is_noncanonical_address(desc_ptr.address)) |
| 3340 | return emulate_gp(ctxt, 0); |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3341 | if (lgdt) |
| 3342 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3343 | else |
| 3344 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3345 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3346 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3347 | return X86EMUL_CONTINUE; |
| 3348 | } |
| 3349 | |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3350 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3351 | { |
| 3352 | return em_lgdt_lidt(ctxt, true); |
| 3353 | } |
| 3354 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3355 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3356 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3357 | int rc; |
| 3358 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3359 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3360 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3361 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3362 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3363 | return rc; |
| 3364 | } |
| 3365 | |
| 3366 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3367 | { |
Nadav Amit | 5b7f6a1 | 2014-11-02 11:54:55 +0200 | [diff] [blame] | 3368 | return em_lgdt_lidt(ctxt, false); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3372 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3373 | if (ctxt->dst.type == OP_MEM) |
| 3374 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3375 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3376 | return X86EMUL_CONTINUE; |
| 3377 | } |
| 3378 | |
| 3379 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3380 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3381 | 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] | 3382 | | (ctxt->src.val & 0x0f)); |
| 3383 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3384 | return X86EMUL_CONTINUE; |
| 3385 | } |
| 3386 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3387 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3388 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3389 | int rc = X86EMUL_CONTINUE; |
| 3390 | |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 3391 | register_address_increment(ctxt, VCPU_REGS_RCX, -1); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3392 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3393 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3394 | rc = jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3395 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3396 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3400 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3401 | int rc = X86EMUL_CONTINUE; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3402 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3403 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
| 3404 | rc = jmp_rel(ctxt, ctxt->src.val); |
| 3405 | |
| 3406 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3407 | } |
| 3408 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3409 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3410 | { |
| 3411 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3412 | &ctxt->dst.val)) |
| 3413 | return X86EMUL_IO_NEEDED; |
| 3414 | |
| 3415 | return X86EMUL_CONTINUE; |
| 3416 | } |
| 3417 | |
| 3418 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3419 | { |
| 3420 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3421 | &ctxt->src.val, 1); |
| 3422 | /* Disable writeback. */ |
| 3423 | ctxt->dst.type = OP_NONE; |
| 3424 | return X86EMUL_CONTINUE; |
| 3425 | } |
| 3426 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3427 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3428 | { |
| 3429 | if (emulator_bad_iopl(ctxt)) |
| 3430 | return emulate_gp(ctxt, 0); |
| 3431 | |
| 3432 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3433 | return X86EMUL_CONTINUE; |
| 3434 | } |
| 3435 | |
| 3436 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3437 | { |
| 3438 | if (emulator_bad_iopl(ctxt)) |
| 3439 | return emulate_gp(ctxt, 0); |
| 3440 | |
| 3441 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3442 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3443 | return X86EMUL_CONTINUE; |
| 3444 | } |
| 3445 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3446 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3447 | { |
| 3448 | u32 eax, ebx, ecx, edx; |
| 3449 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3450 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3451 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3452 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3453 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3454 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3455 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3456 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3457 | return X86EMUL_CONTINUE; |
| 3458 | } |
| 3459 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3460 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3461 | { |
| 3462 | u32 flags; |
| 3463 | |
| 3464 | flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF; |
| 3465 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3466 | |
| 3467 | ctxt->eflags &= ~0xffUL; |
| 3468 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3469 | return X86EMUL_CONTINUE; |
| 3470 | } |
| 3471 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3472 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3473 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3474 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3475 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3476 | return X86EMUL_CONTINUE; |
| 3477 | } |
| 3478 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3479 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3480 | { |
| 3481 | switch (ctxt->op_bytes) { |
| 3482 | #ifdef CONFIG_X86_64 |
| 3483 | case 8: |
| 3484 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3485 | break; |
| 3486 | #endif |
| 3487 | default: |
| 3488 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3489 | break; |
| 3490 | } |
| 3491 | return X86EMUL_CONTINUE; |
| 3492 | } |
| 3493 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3494 | static int em_clflush(struct x86_emulate_ctxt *ctxt) |
| 3495 | { |
| 3496 | /* emulating clflush regardless of cpuid */ |
| 3497 | return X86EMUL_CONTINUE; |
| 3498 | } |
| 3499 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3500 | static bool valid_cr(int nr) |
| 3501 | { |
| 3502 | switch (nr) { |
| 3503 | case 0: |
| 3504 | case 2 ... 4: |
| 3505 | case 8: |
| 3506 | return true; |
| 3507 | default: |
| 3508 | return false; |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3513 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3514 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3515 | return emulate_ud(ctxt); |
| 3516 | |
| 3517 | return X86EMUL_CONTINUE; |
| 3518 | } |
| 3519 | |
| 3520 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3521 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3522 | u64 new_val = ctxt->src.val64; |
| 3523 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3524 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3525 | |
| 3526 | static u64 cr_reserved_bits[] = { |
| 3527 | 0xffffffff00000000ULL, |
| 3528 | 0, 0, 0, /* CR3 checked later */ |
| 3529 | CR4_RESERVED_BITS, |
| 3530 | 0, 0, 0, |
| 3531 | CR8_RESERVED_BITS, |
| 3532 | }; |
| 3533 | |
| 3534 | if (!valid_cr(cr)) |
| 3535 | return emulate_ud(ctxt); |
| 3536 | |
| 3537 | if (new_val & cr_reserved_bits[cr]) |
| 3538 | return emulate_gp(ctxt, 0); |
| 3539 | |
| 3540 | switch (cr) { |
| 3541 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3542 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3543 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3544 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3545 | return emulate_gp(ctxt, 0); |
| 3546 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3547 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3548 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3549 | |
| 3550 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3551 | !(cr4 & X86_CR4_PAE)) |
| 3552 | return emulate_gp(ctxt, 0); |
| 3553 | |
| 3554 | break; |
| 3555 | } |
| 3556 | case 3: { |
| 3557 | u64 rsvd = 0; |
| 3558 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3559 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3560 | if (efer & EFER_LMA) |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 3561 | rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3562 | |
| 3563 | if (new_val & rsvd) |
| 3564 | return emulate_gp(ctxt, 0); |
| 3565 | |
| 3566 | break; |
| 3567 | } |
| 3568 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3569 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3570 | |
| 3571 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3572 | return emulate_gp(ctxt, 0); |
| 3573 | |
| 3574 | break; |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | return X86EMUL_CONTINUE; |
| 3579 | } |
| 3580 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3581 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3582 | { |
| 3583 | unsigned long dr7; |
| 3584 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3585 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3586 | |
| 3587 | /* Check if DR7.Global_Enable is set */ |
| 3588 | return dr7 & (1 << 13); |
| 3589 | } |
| 3590 | |
| 3591 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3592 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3593 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3594 | u64 cr4; |
| 3595 | |
| 3596 | if (dr > 7) |
| 3597 | return emulate_ud(ctxt); |
| 3598 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3599 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3600 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3601 | return emulate_ud(ctxt); |
| 3602 | |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3603 | if (check_dr7_gd(ctxt)) { |
| 3604 | ulong dr6; |
| 3605 | |
| 3606 | ctxt->ops->get_dr(ctxt, 6, &dr6); |
| 3607 | dr6 &= ~15; |
| 3608 | dr6 |= DR6_BD | DR6_RTM; |
| 3609 | ctxt->ops->set_dr(ctxt, 6, dr6); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3610 | return emulate_db(ctxt); |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3611 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3612 | |
| 3613 | return X86EMUL_CONTINUE; |
| 3614 | } |
| 3615 | |
| 3616 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3617 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3618 | u64 new_val = ctxt->src.val64; |
| 3619 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3620 | |
| 3621 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3622 | return emulate_gp(ctxt, 0); |
| 3623 | |
| 3624 | return check_dr_read(ctxt); |
| 3625 | } |
| 3626 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3627 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3628 | { |
| 3629 | u64 efer; |
| 3630 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3631 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3632 | |
| 3633 | if (!(efer & EFER_SVME)) |
| 3634 | return emulate_ud(ctxt); |
| 3635 | |
| 3636 | return X86EMUL_CONTINUE; |
| 3637 | } |
| 3638 | |
| 3639 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3640 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3641 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3642 | |
| 3643 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3644 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3645 | return emulate_gp(ctxt, 0); |
| 3646 | |
| 3647 | return check_svme(ctxt); |
| 3648 | } |
| 3649 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3650 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3651 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3652 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3653 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3654 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3655 | return emulate_ud(ctxt); |
| 3656 | |
| 3657 | return X86EMUL_CONTINUE; |
| 3658 | } |
| 3659 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3660 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3661 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3662 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3663 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3664 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3665 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3666 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3667 | return emulate_gp(ctxt, 0); |
| 3668 | |
| 3669 | return X86EMUL_CONTINUE; |
| 3670 | } |
| 3671 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3672 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3673 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3674 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3675 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3676 | return emulate_gp(ctxt, 0); |
| 3677 | |
| 3678 | return X86EMUL_CONTINUE; |
| 3679 | } |
| 3680 | |
| 3681 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3682 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3683 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3684 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3685 | return emulate_gp(ctxt, 0); |
| 3686 | |
| 3687 | return X86EMUL_CONTINUE; |
| 3688 | } |
| 3689 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3690 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3691 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 3692 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 3693 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3694 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3695 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3696 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3697 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3698 | #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3699 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3700 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3701 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3702 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3703 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3704 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3705 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 3706 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3707 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3708 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3709 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3710 | #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] | 3711 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3712 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3713 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3714 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3715 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3716 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3717 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3718 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3719 | |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3720 | static const struct opcode group7_rm0[] = { |
| 3721 | N, |
| 3722 | I(SrcNone | Priv | EmulateOnUD, em_vmcall), |
| 3723 | N, N, N, N, N, N, |
| 3724 | }; |
| 3725 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3726 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3727 | DI(SrcNone | Priv, monitor), |
| 3728 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3729 | N, N, N, N, N, N, |
| 3730 | }; |
| 3731 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3732 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3733 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3734 | II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3735 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3736 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3737 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3738 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3739 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3740 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3741 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3742 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3743 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3744 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3745 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3746 | N, N, N, N, N, N, |
| 3747 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3748 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3749 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3750 | F(Lock, em_add), |
| 3751 | F(Lock | PageTable, em_or), |
| 3752 | F(Lock, em_adc), |
| 3753 | F(Lock, em_sbb), |
| 3754 | F(Lock | PageTable, em_and), |
| 3755 | F(Lock, em_sub), |
| 3756 | F(Lock, em_xor), |
| 3757 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3758 | }; |
| 3759 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3760 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3761 | I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3762 | }; |
| 3763 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3764 | static const struct opcode group2[] = { |
| 3765 | F(DstMem | ModRM, em_rol), |
| 3766 | F(DstMem | ModRM, em_ror), |
| 3767 | F(DstMem | ModRM, em_rcl), |
| 3768 | F(DstMem | ModRM, em_rcr), |
| 3769 | F(DstMem | ModRM, em_shl), |
| 3770 | F(DstMem | ModRM, em_shr), |
| 3771 | F(DstMem | ModRM, em_shl), |
| 3772 | F(DstMem | ModRM, em_sar), |
| 3773 | }; |
| 3774 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3775 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3776 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3777 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3778 | F(DstMem | SrcNone | Lock, em_not), |
| 3779 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3780 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3781 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3782 | F(DstXacc | Src2Mem, em_div_ex), |
| 3783 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3784 | }; |
| 3785 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3786 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3787 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3788 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3789 | N, N, N, N, N, N, |
| 3790 | }; |
| 3791 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3792 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3793 | F(DstMem | SrcNone | Lock, em_inc), |
| 3794 | F(DstMem | SrcNone | Lock, em_dec), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3795 | I(SrcMem | NearBranch, em_call_near_abs), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3796 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3797 | I(SrcMem | NearBranch, em_jmp_abs), |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 3798 | I(SrcMemFAddr | ImplicitOps, em_jmp_far), |
| 3799 | I(SrcMem | Stack, em_push), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3800 | }; |
| 3801 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3802 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3803 | DI(Prot, sldt), |
| 3804 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3805 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3806 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3807 | N, N, N, N, |
| 3808 | }; |
| 3809 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3810 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3811 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3812 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3813 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3814 | II(SrcMem | Priv, em_lidt, lidt), |
| 3815 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3816 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3817 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3818 | }, { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3819 | EXT(0, group7_rm0), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3820 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3821 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3822 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3823 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3824 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3825 | } }; |
| 3826 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3827 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3828 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3829 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3830 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3831 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3832 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
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 group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3836 | 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] | 3837 | }, { |
| 3838 | N, N, N, N, N, N, N, N, |
| 3839 | } }; |
| 3840 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3841 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3842 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3843 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3844 | }; |
| 3845 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3846 | static const struct gprefix pfx_0f_ae_7 = { |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 3847 | I(SrcMem | ByteOp, em_clflush), N, N, N, |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3848 | }; |
| 3849 | |
| 3850 | static const struct group_dual group15 = { { |
| 3851 | N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7), |
| 3852 | }, { |
| 3853 | N, N, N, N, N, N, N, N, |
| 3854 | } }; |
| 3855 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3856 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3857 | 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] | 3858 | }; |
| 3859 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3860 | static const struct instr_dual instr_dual_0f_2b = { |
| 3861 | I(0, em_mov), N |
| 3862 | }; |
| 3863 | |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 3864 | static const struct gprefix pfx_0f_2b = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3865 | 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] | 3866 | }; |
| 3867 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3868 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3869 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3870 | }; |
| 3871 | |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 3872 | static const struct gprefix pfx_0f_e7 = { |
| 3873 | N, I(Sse, em_mov), N, N, |
| 3874 | }; |
| 3875 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3876 | static const struct escape escape_d9 = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3877 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3878 | }, { |
| 3879 | /* 0xC0 - 0xC7 */ |
| 3880 | N, N, N, N, N, N, N, N, |
| 3881 | /* 0xC8 - 0xCF */ |
| 3882 | N, N, N, N, N, N, N, N, |
| 3883 | /* 0xD0 - 0xC7 */ |
| 3884 | N, N, N, N, N, N, N, N, |
| 3885 | /* 0xD8 - 0xDF */ |
| 3886 | N, N, N, N, N, N, N, N, |
| 3887 | /* 0xE0 - 0xE7 */ |
| 3888 | N, N, N, N, N, N, N, N, |
| 3889 | /* 0xE8 - 0xEF */ |
| 3890 | N, N, N, N, N, N, N, N, |
| 3891 | /* 0xF0 - 0xF7 */ |
| 3892 | N, N, N, N, N, N, N, N, |
| 3893 | /* 0xF8 - 0xFF */ |
| 3894 | N, N, N, N, N, N, N, N, |
| 3895 | } }; |
| 3896 | |
| 3897 | static const struct escape escape_db = { { |
| 3898 | N, N, N, N, N, N, N, N, |
| 3899 | }, { |
| 3900 | /* 0xC0 - 0xC7 */ |
| 3901 | N, N, N, N, N, N, N, N, |
| 3902 | /* 0xC8 - 0xCF */ |
| 3903 | N, N, N, N, N, N, N, N, |
| 3904 | /* 0xD0 - 0xC7 */ |
| 3905 | N, N, N, N, N, N, N, N, |
| 3906 | /* 0xD8 - 0xDF */ |
| 3907 | N, N, N, N, N, N, N, N, |
| 3908 | /* 0xE0 - 0xE7 */ |
| 3909 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3910 | /* 0xE8 - 0xEF */ |
| 3911 | N, N, N, N, N, N, N, N, |
| 3912 | /* 0xF0 - 0xF7 */ |
| 3913 | N, N, N, N, N, N, N, N, |
| 3914 | /* 0xF8 - 0xFF */ |
| 3915 | N, N, N, N, N, N, N, N, |
| 3916 | } }; |
| 3917 | |
| 3918 | static const struct escape escape_dd = { { |
Nadav Amit | 16bebef | 2014-12-25 02:52:18 +0200 | [diff] [blame] | 3919 | N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw), |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3920 | }, { |
| 3921 | /* 0xC0 - 0xC7 */ |
| 3922 | N, N, N, N, N, N, N, N, |
| 3923 | /* 0xC8 - 0xCF */ |
| 3924 | N, N, N, N, N, N, N, N, |
| 3925 | /* 0xD0 - 0xC7 */ |
| 3926 | N, N, N, N, N, N, N, N, |
| 3927 | /* 0xD8 - 0xDF */ |
| 3928 | N, N, N, N, N, N, N, N, |
| 3929 | /* 0xE0 - 0xE7 */ |
| 3930 | N, N, N, N, N, N, N, N, |
| 3931 | /* 0xE8 - 0xEF */ |
| 3932 | N, N, N, N, N, N, N, N, |
| 3933 | /* 0xF0 - 0xF7 */ |
| 3934 | N, N, N, N, N, N, N, N, |
| 3935 | /* 0xF8 - 0xFF */ |
| 3936 | N, N, N, N, N, N, N, N, |
| 3937 | } }; |
| 3938 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 3939 | static const struct instr_dual instr_dual_0f_c3 = { |
| 3940 | I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N |
| 3941 | }; |
| 3942 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3943 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3944 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3945 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3946 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3947 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3948 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3949 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3950 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3951 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3952 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3953 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3954 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3955 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3956 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3957 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3958 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3959 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3960 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3961 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3962 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3963 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3964 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3965 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3966 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3967 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3968 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3969 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3970 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3971 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3972 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3973 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3974 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3975 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3976 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3977 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3978 | N, N, N, N, |
| 3979 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3980 | I(SrcImm | Mov | Stack, em_push), |
| 3981 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3982 | I(SrcImmByte | Mov | Stack, em_push), |
| 3983 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3984 | 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] | 3985 | 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] | 3986 | /* 0x70 - 0x7F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3987 | X16(D(SrcImmByte | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3988 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3989 | G(ByteOp | DstMem | SrcImm, group1), |
| 3990 | G(DstMem | SrcImm, group1), |
| 3991 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3992 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3993 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3994 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3995 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3996 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3997 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3998 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3999 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 4000 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 4001 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4002 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4003 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4004 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 4005 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 4006 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 4007 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 4008 | II(ImplicitOps | Stack, em_popf, popf), |
| 4009 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4010 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4011 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4012 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4013 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4014 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4015 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4016 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4017 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 4018 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4019 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4020 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4021 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4022 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4023 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4024 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4025 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4026 | I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm), |
| 4027 | I(ImplicitOps | NearBranch, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4028 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 4029 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4030 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4031 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 4032 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 4033 | I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm), |
| 4034 | I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4035 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4036 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4037 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4038 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 4039 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 4040 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 4041 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 4042 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4043 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4044 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4045 | 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] | 4046 | /* 0xE0 - 0xE7 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4047 | X3(I(SrcImmByte | NearBranch, em_loop)), |
| 4048 | I(SrcImmByte | NearBranch, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4049 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 4050 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4051 | /* 0xE8 - 0xEF */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4052 | I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch), |
| 4053 | I(SrcImmFAddr | No64, em_jmp_far), |
| 4054 | D(SrcImmByte | ImplicitOps | NearBranch), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4055 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 4056 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4057 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4058 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4059 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 4060 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4061 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 4062 | D(ImplicitOps), D(ImplicitOps), |
| 4063 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4064 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 4065 | }; |
| 4066 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4067 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4068 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4069 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4070 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4071 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4072 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4073 | N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4074 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4075 | N, N, N, N, N, N, N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4076 | D(ImplicitOps | ModRM | SrcMem | NoAccess), |
| 4077 | N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4078 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 4079 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 4080 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 4081 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 4082 | check_cr_write), |
| 4083 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 4084 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4085 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4086 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 4087 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4088 | N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4089 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4090 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4091 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4092 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4093 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4094 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4095 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 4096 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4097 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4098 | N, N, N, N, N, N, N, N, |
| 4099 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4100 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4101 | /* 0x50 - 0x5F */ |
| 4102 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4103 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4104 | N, N, N, N, |
| 4105 | N, N, N, N, |
| 4106 | N, N, N, N, |
| 4107 | 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] | 4108 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4109 | N, N, N, N, |
| 4110 | N, N, N, N, |
| 4111 | N, N, N, N, |
| 4112 | 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] | 4113 | /* 0x80 - 0x8F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4114 | X16(D(SrcImm | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4115 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4116 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4117 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4118 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4119 | II(ImplicitOps, em_cpuid, cpuid), |
| 4120 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4121 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4122 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4123 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4124 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4125 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4126 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4127 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4128 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4129 | GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4130 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 4131 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4132 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4133 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4134 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4135 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4136 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4137 | /* 0xB8 - 0xBF */ |
| 4138 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4139 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4140 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 4141 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4142 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4143 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 4144 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4145 | N, ID(0, &instr_dual_0f_c3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4146 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4147 | /* 0xC8 - 0xCF */ |
| 4148 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4149 | /* 0xD0 - 0xDF */ |
| 4150 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4151 | /* 0xE0 - 0xEF */ |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4152 | N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), |
| 4153 | N, N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4154 | /* 0xF0 - 0xFF */ |
| 4155 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4156 | }; |
| 4157 | |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4158 | static const struct instr_dual instr_dual_0f_38_f0 = { |
| 4159 | I(DstReg | SrcMem | Mov, em_movbe), N |
| 4160 | }; |
| 4161 | |
| 4162 | static const struct instr_dual instr_dual_0f_38_f1 = { |
| 4163 | I(DstMem | SrcReg | Mov, em_movbe), N |
| 4164 | }; |
| 4165 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4166 | static const struct gprefix three_byte_0f_38_f0 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4167 | ID(0, &instr_dual_0f_38_f0), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4168 | }; |
| 4169 | |
| 4170 | static const struct gprefix three_byte_0f_38_f1 = { |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4171 | ID(0, &instr_dual_0f_38_f1), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4172 | }; |
| 4173 | |
| 4174 | /* |
| 4175 | * Insns below are selected by the prefix which indexed by the third opcode |
| 4176 | * byte. |
| 4177 | */ |
| 4178 | static const struct opcode opcode_map_0f_38[256] = { |
| 4179 | /* 0x00 - 0x7f */ |
| 4180 | 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] | 4181 | /* 0x80 - 0xef */ |
| 4182 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 4183 | /* 0xf0 - 0xf1 */ |
Nadav Amit | 53bb4f7 | 2014-12-07 11:49:42 +0200 | [diff] [blame] | 4184 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), |
| 4185 | GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1), |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4186 | /* 0xf2 - 0xff */ |
| 4187 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4188 | }; |
| 4189 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4190 | #undef D |
| 4191 | #undef N |
| 4192 | #undef G |
| 4193 | #undef GD |
| 4194 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4195 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4196 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4197 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4198 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4199 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4200 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4201 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4202 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4203 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4204 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4205 | { |
| 4206 | unsigned size; |
| 4207 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4208 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4209 | if (size == 8) |
| 4210 | size = 4; |
| 4211 | return size; |
| 4212 | } |
| 4213 | |
| 4214 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4215 | unsigned size, bool sign_extension) |
| 4216 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4217 | int rc = X86EMUL_CONTINUE; |
| 4218 | |
| 4219 | op->type = OP_IMM; |
| 4220 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4221 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4222 | /* NB. Immediates are sign-extended as necessary. */ |
| 4223 | switch (op->bytes) { |
| 4224 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4225 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4226 | break; |
| 4227 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4228 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4229 | break; |
| 4230 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4231 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4232 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4233 | case 8: |
| 4234 | op->val = insn_fetch(s64, ctxt); |
| 4235 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4236 | } |
| 4237 | if (!sign_extension) { |
| 4238 | switch (op->bytes) { |
| 4239 | case 1: |
| 4240 | op->val &= 0xff; |
| 4241 | break; |
| 4242 | case 2: |
| 4243 | op->val &= 0xffff; |
| 4244 | break; |
| 4245 | case 4: |
| 4246 | op->val &= 0xffffffff; |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
| 4250 | done: |
| 4251 | return rc; |
| 4252 | } |
| 4253 | |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4254 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4255 | unsigned d) |
| 4256 | { |
| 4257 | int rc = X86EMUL_CONTINUE; |
| 4258 | |
| 4259 | switch (d) { |
| 4260 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4261 | decode_register_operand(ctxt, op); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4262 | break; |
| 4263 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4264 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4265 | break; |
| 4266 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4267 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4268 | mem_common: |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4269 | *op = ctxt->memop; |
| 4270 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4271 | if (ctxt->d & BitOp) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4272 | fetch_bit_operand(ctxt); |
| 4273 | op->orig_val = op->val; |
| 4274 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4275 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4276 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4277 | goto mem_common; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4278 | case OpAcc: |
| 4279 | op->type = OP_REG; |
| 4280 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4281 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4282 | fetch_register_operand(op); |
| 4283 | op->orig_val = op->val; |
| 4284 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4285 | case OpAccLo: |
| 4286 | op->type = OP_REG; |
| 4287 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4288 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4289 | fetch_register_operand(op); |
| 4290 | op->orig_val = op->val; |
| 4291 | break; |
| 4292 | case OpAccHi: |
| 4293 | if (ctxt->d & ByteOp) { |
| 4294 | op->type = OP_NONE; |
| 4295 | break; |
| 4296 | } |
| 4297 | op->type = OP_REG; |
| 4298 | op->bytes = ctxt->op_bytes; |
| 4299 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4300 | fetch_register_operand(op); |
| 4301 | op->orig_val = op->val; |
| 4302 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4303 | case OpDI: |
| 4304 | op->type = OP_MEM; |
| 4305 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4306 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4307 | register_address(ctxt, VCPU_REGS_RDI); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4308 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4309 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4310 | op->count = 1; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4311 | break; |
| 4312 | case OpDX: |
| 4313 | op->type = OP_REG; |
| 4314 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4315 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4316 | fetch_register_operand(op); |
| 4317 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4318 | case OpCL: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4319 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4320 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4321 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4322 | break; |
| 4323 | case OpImmByte: |
| 4324 | rc = decode_imm(ctxt, op, 1, true); |
| 4325 | break; |
| 4326 | case OpOne: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4327 | op->type = OP_IMM; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4328 | op->bytes = 1; |
| 4329 | op->val = 1; |
| 4330 | break; |
| 4331 | case OpImm: |
| 4332 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4333 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4334 | case OpImm64: |
| 4335 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4336 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4337 | case OpMem8: |
| 4338 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4339 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4340 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4341 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4342 | fetch_register_operand(&ctxt->memop); |
| 4343 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4344 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4345 | case OpMem16: |
| 4346 | ctxt->memop.bytes = 2; |
| 4347 | goto mem_common; |
| 4348 | case OpMem32: |
| 4349 | ctxt->memop.bytes = 4; |
| 4350 | goto mem_common; |
| 4351 | case OpImmU16: |
| 4352 | rc = decode_imm(ctxt, op, 2, false); |
| 4353 | break; |
| 4354 | case OpImmU: |
| 4355 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4356 | break; |
| 4357 | case OpSI: |
| 4358 | op->type = OP_MEM; |
| 4359 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4360 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4361 | register_address(ctxt, VCPU_REGS_RSI); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4362 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4363 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4364 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4365 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4366 | case OpXLat: |
| 4367 | op->type = OP_MEM; |
| 4368 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4369 | op->addr.mem.ea = |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 4370 | address_mask(ctxt, |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4371 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4372 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4373 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4374 | op->val = 0; |
| 4375 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4376 | case OpImmFAddr: |
| 4377 | op->type = OP_IMM; |
| 4378 | op->addr.mem.ea = ctxt->_eip; |
| 4379 | op->bytes = ctxt->op_bytes + 2; |
| 4380 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4381 | break; |
| 4382 | case OpMemFAddr: |
| 4383 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4384 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4385 | case OpES: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4386 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4387 | op->val = VCPU_SREG_ES; |
| 4388 | break; |
| 4389 | case OpCS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4390 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4391 | op->val = VCPU_SREG_CS; |
| 4392 | break; |
| 4393 | case OpSS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4394 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4395 | op->val = VCPU_SREG_SS; |
| 4396 | break; |
| 4397 | case OpDS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4398 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4399 | op->val = VCPU_SREG_DS; |
| 4400 | break; |
| 4401 | case OpFS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4402 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4403 | op->val = VCPU_SREG_FS; |
| 4404 | break; |
| 4405 | case OpGS: |
Nadav Amit | d29b9d7 | 2014-11-02 11:54:47 +0200 | [diff] [blame] | 4406 | op->type = OP_IMM; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4407 | op->val = VCPU_SREG_GS; |
| 4408 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4409 | case OpImplicit: |
| 4410 | /* Special instructions do their own operand decoding. */ |
| 4411 | default: |
| 4412 | op->type = OP_NONE; /* Disable writeback. */ |
| 4413 | break; |
| 4414 | } |
| 4415 | |
| 4416 | done: |
| 4417 | return rc; |
| 4418 | } |
| 4419 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4420 | 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] | 4421 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4422 | int rc = X86EMUL_CONTINUE; |
| 4423 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4424 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4425 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4426 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4427 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4428 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4429 | ctxt->memop.type = OP_NONE; |
| 4430 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4431 | ctxt->_eip = ctxt->eip; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 4432 | ctxt->fetch.ptr = ctxt->fetch.data; |
| 4433 | ctxt->fetch.end = ctxt->fetch.data + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4434 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4435 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4436 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4437 | else { |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 4438 | rc = __do_insn_fetch_bytes(ctxt, 1); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4439 | if (rc != X86EMUL_CONTINUE) |
| 4440 | return rc; |
| 4441 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4442 | |
| 4443 | switch (mode) { |
| 4444 | case X86EMUL_MODE_REAL: |
| 4445 | case X86EMUL_MODE_VM86: |
| 4446 | case X86EMUL_MODE_PROT16: |
| 4447 | def_op_bytes = def_ad_bytes = 2; |
| 4448 | break; |
| 4449 | case X86EMUL_MODE_PROT32: |
| 4450 | def_op_bytes = def_ad_bytes = 4; |
| 4451 | break; |
| 4452 | #ifdef CONFIG_X86_64 |
| 4453 | case X86EMUL_MODE_PROT64: |
| 4454 | def_op_bytes = 4; |
| 4455 | def_ad_bytes = 8; |
| 4456 | break; |
| 4457 | #endif |
| 4458 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4459 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4460 | } |
| 4461 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4462 | ctxt->op_bytes = def_op_bytes; |
| 4463 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4464 | |
| 4465 | /* Legacy prefixes. */ |
| 4466 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4467 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4468 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4469 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4470 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4471 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4472 | break; |
| 4473 | case 0x67: /* address-size override */ |
| 4474 | if (mode == X86EMUL_MODE_PROT64) |
| 4475 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4476 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4477 | else |
| 4478 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4479 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4480 | break; |
| 4481 | case 0x26: /* ES override */ |
| 4482 | case 0x2e: /* CS override */ |
| 4483 | case 0x36: /* SS override */ |
| 4484 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4485 | has_seg_override = true; |
| 4486 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4487 | break; |
| 4488 | case 0x64: /* FS override */ |
| 4489 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4490 | has_seg_override = true; |
| 4491 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4492 | break; |
| 4493 | case 0x40 ... 0x4f: /* REX */ |
| 4494 | if (mode != X86EMUL_MODE_PROT64) |
| 4495 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4496 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4497 | continue; |
| 4498 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4499 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4500 | break; |
| 4501 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4502 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4503 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4504 | break; |
| 4505 | default: |
| 4506 | goto done_prefixes; |
| 4507 | } |
| 4508 | |
| 4509 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4510 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4511 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | done_prefixes: |
| 4515 | |
| 4516 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4517 | if (ctxt->rex_prefix & 8) |
| 4518 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4519 | |
| 4520 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4521 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4522 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4523 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4524 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4525 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4526 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4527 | |
| 4528 | /* 0F_38 opcode map */ |
| 4529 | if (ctxt->b == 0x38) { |
| 4530 | ctxt->opcode_len = 3; |
| 4531 | ctxt->b = insn_fetch(u8, ctxt); |
| 4532 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4533 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4534 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4535 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4536 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4537 | if (ctxt->d & ModRM) |
| 4538 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4539 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4540 | /* vex-prefix instructions are not implemented */ |
| 4541 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
Nadav Amit | d14cb5d | 2014-11-02 11:54:58 +0200 | [diff] [blame] | 4542 | (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) { |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4543 | ctxt->d = NotImpl; |
| 4544 | } |
| 4545 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4546 | while (ctxt->d & GroupMask) { |
| 4547 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4548 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4549 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4550 | opcode = opcode.u.group[goffset]; |
| 4551 | break; |
| 4552 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4553 | goffset = (ctxt->modrm >> 3) & 7; |
| 4554 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4555 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4556 | else |
| 4557 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4558 | break; |
| 4559 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4560 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4561 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4562 | break; |
| 4563 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4564 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4565 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4566 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4567 | switch (simd_prefix) { |
| 4568 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4569 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4570 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4571 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4572 | } |
| 4573 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4574 | case Escape: |
| 4575 | if (ctxt->modrm > 0xbf) |
| 4576 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4577 | else |
| 4578 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4579 | break; |
Nadav Amit | 39f062f | 2014-11-26 15:47:18 +0200 | [diff] [blame] | 4580 | case InstrDual: |
| 4581 | if ((ctxt->modrm >> 6) == 3) |
| 4582 | opcode = opcode.u.idual->mod3; |
| 4583 | else |
| 4584 | opcode = opcode.u.idual->mod012; |
| 4585 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4586 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4587 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4588 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4589 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4590 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4591 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4592 | } |
| 4593 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 4594 | /* Unrecognised? */ |
| 4595 | if (ctxt->d == 0) |
| 4596 | return EMULATION_FAILED; |
| 4597 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4598 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4599 | |
Nadav Amit | 3a6095a | 2014-08-13 16:50:13 +0300 | [diff] [blame] | 4600 | if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) |
| 4601 | return EMULATION_FAILED; |
| 4602 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4603 | if (unlikely(ctxt->d & |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4604 | (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch| |
| 4605 | No16))) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4606 | /* |
| 4607 | * These are copied unconditionally here, and checked unconditionally |
| 4608 | * in x86_emulate_insn. |
| 4609 | */ |
| 4610 | ctxt->check_perm = opcode.check_perm; |
| 4611 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4612 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4613 | if (ctxt->d & NotImpl) |
| 4614 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4615 | |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4616 | if (mode == X86EMUL_MODE_PROT64) { |
| 4617 | if (ctxt->op_bytes == 4 && (ctxt->d & Stack)) |
| 4618 | ctxt->op_bytes = 8; |
| 4619 | else if (ctxt->d & NearBranch) |
| 4620 | ctxt->op_bytes = 8; |
| 4621 | } |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4622 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4623 | if (ctxt->d & Op3264) { |
| 4624 | if (mode == X86EMUL_MODE_PROT64) |
| 4625 | ctxt->op_bytes = 8; |
| 4626 | else |
| 4627 | ctxt->op_bytes = 4; |
| 4628 | } |
| 4629 | |
Nadav Amit | ed9aad2 | 2014-11-02 11:55:00 +0200 | [diff] [blame] | 4630 | if ((ctxt->d & No16) && ctxt->op_bytes == 2) |
| 4631 | ctxt->op_bytes = 4; |
| 4632 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4633 | if (ctxt->d & Sse) |
| 4634 | ctxt->op_bytes = 16; |
| 4635 | else if (ctxt->d & Mmx) |
| 4636 | ctxt->op_bytes = 8; |
| 4637 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4638 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4639 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4640 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4641 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4642 | if (!has_seg_override) { |
| 4643 | has_seg_override = true; |
| 4644 | ctxt->seg_override = ctxt->modrm_seg; |
| 4645 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4646 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4647 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4648 | if (rc != X86EMUL_CONTINUE) |
| 4649 | goto done; |
| 4650 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4651 | if (!has_seg_override) |
| 4652 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4653 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4654 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4655 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4656 | /* |
| 4657 | * Decode and fetch the source operand: register, memory |
| 4658 | * or immediate. |
| 4659 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4660 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4661 | if (rc != X86EMUL_CONTINUE) |
| 4662 | goto done; |
| 4663 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4664 | /* |
| 4665 | * Decode and fetch the second source operand: register, memory |
| 4666 | * or immediate. |
| 4667 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4668 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4669 | if (rc != X86EMUL_CONTINUE) |
| 4670 | goto done; |
| 4671 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4672 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4673 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4674 | |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 4675 | if (ctxt->rip_relative) |
Nadav Amit | 1c1c35a | 2014-11-19 17:43:09 +0200 | [diff] [blame] | 4676 | ctxt->memopp->addr.mem.ea = address_mask(ctxt, |
| 4677 | ctxt->memopp->addr.mem.ea + ctxt->_eip); |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4678 | |
Paolo Bonzini | a430c91 | 2014-10-23 14:54:14 +0200 | [diff] [blame] | 4679 | done: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4680 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4681 | } |
| 4682 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4683 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4684 | { |
| 4685 | return ctxt->d & PageTable; |
| 4686 | } |
| 4687 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4688 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4689 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4690 | /* The second termination condition only applies for REPE |
| 4691 | * and REPNE. Test if the repeat string operation prefix is |
| 4692 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4693 | * corresponding termination condition according to: |
| 4694 | * - if REPE/REPZ and ZF = 0 then done |
| 4695 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4696 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4697 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4698 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4699 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4700 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4701 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4702 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4703 | return true; |
| 4704 | |
| 4705 | return false; |
| 4706 | } |
| 4707 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4708 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4709 | { |
| 4710 | bool fault = false; |
| 4711 | |
| 4712 | ctxt->ops->get_fpu(ctxt); |
| 4713 | asm volatile("1: fwait \n\t" |
| 4714 | "2: \n\t" |
| 4715 | ".pushsection .fixup,\"ax\" \n\t" |
| 4716 | "3: \n\t" |
| 4717 | "movb $1, %[fault] \n\t" |
| 4718 | "jmp 2b \n\t" |
| 4719 | ".popsection \n\t" |
| 4720 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4721 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4722 | ctxt->ops->put_fpu(ctxt); |
| 4723 | |
| 4724 | if (unlikely(fault)) |
| 4725 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4726 | |
| 4727 | return X86EMUL_CONTINUE; |
| 4728 | } |
| 4729 | |
| 4730 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4731 | struct operand *op) |
| 4732 | { |
| 4733 | if (op->type == OP_MM) |
| 4734 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4735 | } |
| 4736 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4737 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4738 | { |
| 4739 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4740 | if (!(ctxt->d & ByteOp)) |
| 4741 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4742 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4743 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4744 | [fastop]"+S"(fop) |
| 4745 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4746 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4747 | if (!fop) /* exception is returned in fop variable */ |
| 4748 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4749 | return X86EMUL_CONTINUE; |
| 4750 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4751 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4752 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 4753 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4754 | memset(&ctxt->rip_relative, 0, |
| 4755 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4756 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4757 | ctxt->io_read.pos = 0; |
| 4758 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4759 | ctxt->mem_read.end = 0; |
| 4760 | } |
| 4761 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4762 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4763 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4764 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4765 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4766 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4767 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4768 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4769 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4770 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4771 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4772 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4773 | goto done; |
| 4774 | } |
| 4775 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4776 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4777 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4778 | goto done; |
| 4779 | } |
| 4780 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4781 | if (unlikely(ctxt->d & |
| 4782 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 4783 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4784 | (ctxt->d & Undefined)) { |
| 4785 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4786 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4787 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4788 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4789 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4790 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 4791 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4792 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4793 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4794 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4795 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 4796 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4797 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4798 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4799 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4800 | if (ctxt->d & Mmx) { |
| 4801 | rc = flush_pending_x87_faults(ctxt); |
| 4802 | if (rc != X86EMUL_CONTINUE) |
| 4803 | goto done; |
| 4804 | /* |
| 4805 | * Now that we know the fpu is exception safe, we can fetch |
| 4806 | * operands from it. |
| 4807 | */ |
| 4808 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4809 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4810 | if (!(ctxt->d & Mov)) |
| 4811 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4812 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4813 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4814 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4815 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4816 | X86_ICPT_PRE_EXCEPT); |
| 4817 | if (rc != X86EMUL_CONTINUE) |
| 4818 | goto done; |
| 4819 | } |
| 4820 | |
Nadav Amit | 64a3829 | 2014-12-10 11:19:04 +0200 | [diff] [blame] | 4821 | /* Instruction can only be executed in protected mode */ |
| 4822 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 4823 | rc = emulate_ud(ctxt); |
| 4824 | goto done; |
| 4825 | } |
| 4826 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4827 | /* Privileged instruction can be executed only in CPL=0 */ |
| 4828 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 4829 | if (ctxt->d & PrivUD) |
| 4830 | rc = emulate_ud(ctxt); |
| 4831 | else |
| 4832 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4833 | goto done; |
| 4834 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4835 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4836 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4837 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4838 | rc = ctxt->check_perm(ctxt); |
| 4839 | if (rc != X86EMUL_CONTINUE) |
| 4840 | goto done; |
| 4841 | } |
| 4842 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4843 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4844 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4845 | X86_ICPT_POST_EXCEPT); |
| 4846 | if (rc != X86EMUL_CONTINUE) |
| 4847 | goto done; |
| 4848 | } |
| 4849 | |
| 4850 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 4851 | /* All REP prefixes have the same first termination condition */ |
| 4852 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
| 4853 | ctxt->eip = ctxt->_eip; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4854 | ctxt->eflags &= ~EFLG_RF; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4855 | goto done; |
| 4856 | } |
| 4857 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4858 | } |
| 4859 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4860 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4861 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4862 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4863 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4864 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4865 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4866 | } |
| 4867 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4868 | if (ctxt->src2.type == OP_MEM) { |
| 4869 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4870 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4871 | if (rc != X86EMUL_CONTINUE) |
| 4872 | goto done; |
| 4873 | } |
| 4874 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4875 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4876 | goto special_insn; |
| 4877 | |
| 4878 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4879 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4880 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4881 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4882 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4883 | if (rc != X86EMUL_CONTINUE) |
| 4884 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4885 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4886 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4887 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4888 | special_insn: |
| 4889 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4890 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4891 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4892 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4893 | if (rc != X86EMUL_CONTINUE) |
| 4894 | goto done; |
| 4895 | } |
| 4896 | |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 4897 | if (ctxt->rep_prefix && (ctxt->d & String)) |
| 4898 | ctxt->eflags |= EFLG_RF; |
| 4899 | else |
| 4900 | ctxt->eflags &= ~EFLG_RF; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4901 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4902 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4903 | if (ctxt->d & Fastop) { |
| 4904 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4905 | rc = fastop(ctxt, fop); |
| 4906 | if (rc != X86EMUL_CONTINUE) |
| 4907 | goto done; |
| 4908 | goto writeback; |
| 4909 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4910 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4911 | if (rc != X86EMUL_CONTINUE) |
| 4912 | goto done; |
| 4913 | goto writeback; |
| 4914 | } |
| 4915 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4916 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4917 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4918 | else if (ctxt->opcode_len == 3) |
| 4919 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4920 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4921 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4922 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4923 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4924 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4925 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4926 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4927 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4928 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4929 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4930 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4931 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4932 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4933 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4934 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4935 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 4936 | ctxt->dst.type = OP_NONE; |
| 4937 | else |
| 4938 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4939 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4940 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4941 | switch (ctxt->op_bytes) { |
| 4942 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4943 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4944 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4945 | } |
| 4946 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4947 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4948 | rc = emulate_int(ctxt, 3); |
| 4949 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4950 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4951 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4952 | break; |
| 4953 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4954 | if (ctxt->eflags & EFLG_OF) |
| 4955 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4956 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4957 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4958 | case 0xeb: /* jmp rel short */ |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4959 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4960 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4961 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4962 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4963 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4964 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4965 | case 0xf5: /* cmc */ |
| 4966 | /* complement carry flag from eflags reg */ |
| 4967 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4968 | break; |
| 4969 | case 0xf8: /* clc */ |
| 4970 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4971 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4972 | case 0xf9: /* stc */ |
| 4973 | ctxt->eflags |= EFLG_CF; |
| 4974 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4975 | case 0xfc: /* cld */ |
| 4976 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4977 | break; |
| 4978 | case 0xfd: /* std */ |
| 4979 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4980 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4981 | default: |
| 4982 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4983 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4984 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4985 | if (rc != X86EMUL_CONTINUE) |
| 4986 | goto done; |
| 4987 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4988 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 4989 | if (ctxt->d & SrcWrite) { |
| 4990 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 4991 | rc = writeback(ctxt, &ctxt->src); |
| 4992 | if (rc != X86EMUL_CONTINUE) |
| 4993 | goto done; |
| 4994 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 4995 | if (!(ctxt->d & NoWrite)) { |
| 4996 | rc = writeback(ctxt, &ctxt->dst); |
| 4997 | if (rc != X86EMUL_CONTINUE) |
| 4998 | goto done; |
| 4999 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5000 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5001 | /* |
| 5002 | * restore dst type in case the decoding will be reused |
| 5003 | * (happens for string instruction ) |
| 5004 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5005 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5006 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5007 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5008 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 5009 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5010 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 5011 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 5012 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5013 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5014 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5015 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 5016 | if ((ctxt->d & SrcMask) == SrcSI) |
| 5017 | count = ctxt->src.count; |
| 5018 | else |
| 5019 | count = ctxt->dst.count; |
Paolo Bonzini | 01485a2 | 2014-11-19 18:25:08 +0100 | [diff] [blame] | 5020 | register_address_increment(ctxt, VCPU_REGS_RCX, -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 5021 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5022 | if (!string_insn_completed(ctxt)) { |
| 5023 | /* |
| 5024 | * Re-enter guest when pio read ahead buffer is empty |
| 5025 | * or, if it is not used, after each 1024 iteration. |
| 5026 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5027 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5028 | (r->end == 0 || r->end != r->pos)) { |
| 5029 | /* |
| 5030 | * Reset read cache. Usually happens before |
| 5031 | * decode, but since instruction is restarted |
| 5032 | * we have to do it here. |
| 5033 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5034 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5035 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5036 | return EMULATION_RESTART; |
| 5037 | } |
| 5038 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 5039 | } |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 5040 | ctxt->eflags &= ~EFLG_RF; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5041 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5042 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5043 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5044 | |
| 5045 | done: |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5046 | if (rc == X86EMUL_PROPAGATE_FAULT) { |
| 5047 | WARN_ON(ctxt->exception.vector > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 5048 | ctxt->have_exception = true; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5049 | } |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 5050 | if (rc == X86EMUL_INTERCEPTED) |
| 5051 | return EMULATION_INTERCEPTED; |
| 5052 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5053 | if (rc == X86EMUL_CONTINUE) |
| 5054 | writeback_registers(ctxt); |
| 5055 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5056 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5057 | |
| 5058 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5059 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5060 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 5061 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5062 | break; |
| 5063 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5064 | case 0x0d: /* GrpP (prefetch) */ |
| 5065 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 5066 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5067 | break; |
| 5068 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5069 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5070 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5071 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5072 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5073 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5074 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 5075 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 5076 | ctxt->dst.val = ctxt->src.val; |
| 5077 | else if (ctxt->mode != X86EMUL_MODE_PROT64 || |
| 5078 | ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5079 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5080 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5081 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5082 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5083 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5084 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5085 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5086 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5087 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5088 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5089 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5090 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5091 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5092 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5093 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5094 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5095 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5096 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5097 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5098 | default: |
| 5099 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5100 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5101 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5102 | threebyte_insn: |
| 5103 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5104 | if (rc != X86EMUL_CONTINUE) |
| 5105 | goto done; |
| 5106 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5107 | goto writeback; |
| 5108 | |
| 5109 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 5110 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5111 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5112 | |
| 5113 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5114 | { |
| 5115 | invalidate_registers(ctxt); |
| 5116 | } |
| 5117 | |
| 5118 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5119 | { |
| 5120 | writeback_registers(ctxt); |
| 5121 | } |