Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 29 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 30 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | /* |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
| 89 | #define DstImmUByte (OpImmUByte << DstShift) |
| 90 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 91 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 92 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 94 | #define SrcShift 6 |
| 95 | #define SrcNone (OpNone << SrcShift) |
| 96 | #define SrcReg (OpReg << SrcShift) |
| 97 | #define SrcMem (OpMem << SrcShift) |
| 98 | #define SrcMem16 (OpMem16 << SrcShift) |
| 99 | #define SrcMem32 (OpMem32 << SrcShift) |
| 100 | #define SrcImm (OpImm << SrcShift) |
| 101 | #define SrcImmByte (OpImmByte << SrcShift) |
| 102 | #define SrcOne (OpOne << SrcShift) |
| 103 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 104 | #define SrcImmU (OpImmU << SrcShift) |
| 105 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 106 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 107 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 108 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 109 | #define SrcAcc (OpAcc << SrcShift) |
| 110 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 111 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 112 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 113 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 114 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 115 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 116 | #define BitOp (1<<11) |
| 117 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 118 | #define String (1<<13) /* String instruction (rep capable) */ |
| 119 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 120 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 121 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 122 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 123 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 124 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 125 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 126 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 127 | /* Generic ModRM decode. */ |
| 128 | #define ModRM (1<<19) |
| 129 | /* Destination is only written; never read. */ |
| 130 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 131 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 132 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 133 | #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 134 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 135 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 136 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 137 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 138 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 139 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 140 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 141 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 142 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 143 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 144 | #define Src2None (OpNone << Src2Shift) |
Avi Kivity | ab2c5ce | 2013-02-09 11:31:46 +0200 | [diff] [blame] | 145 | #define Src2Mem (OpMem << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 146 | #define Src2CL (OpCL << Src2Shift) |
| 147 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 148 | #define Src2One (OpOne << Src2Shift) |
| 149 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 150 | #define Src2ES (OpES << Src2Shift) |
| 151 | #define Src2CS (OpCS << Src2Shift) |
| 152 | #define Src2SS (OpSS << Src2Shift) |
| 153 | #define Src2DS (OpDS << Src2Shift) |
| 154 | #define Src2FS (OpFS << Src2Shift) |
| 155 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 156 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 157 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 158 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 159 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 160 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 161 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 162 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 163 | #define SrcWrite ((u64)1 << 46) /* Write back src operand */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 164 | #define NoMod ((u64)1 << 47) /* Mod field is ignored */ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 165 | #define Intercept ((u64)1 << 48) /* Has valid intercept field */ |
| 166 | #define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */ |
Nadav Amit | 10e38fc | 2014-06-18 17:19:34 +0300 | [diff] [blame] | 167 | #define NoBigReal ((u64)1 << 50) /* No big real mode */ |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 168 | #define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 169 | #define NearBranch ((u64)1 << 52) /* Near branches */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 170 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 171 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 172 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 173 | #define X2(x...) x, x |
| 174 | #define X3(x...) X2(x), x |
| 175 | #define X4(x...) X2(x), X2(x) |
| 176 | #define X5(x...) X4(x), x |
| 177 | #define X6(x...) X4(x), X2(x) |
| 178 | #define X7(x...) X4(x), X3(x) |
| 179 | #define X8(x...) X4(x), X4(x) |
| 180 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 181 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 182 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 183 | #define FASTOP_SIZE 8 |
| 184 | |
| 185 | /* |
| 186 | * fastop functions have a special calling convention: |
| 187 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 188 | * dst: rax (in/out) |
| 189 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 190 | * src2: rcx (in) |
| 191 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 192 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 193 | * |
| 194 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 195 | * different operand sizes can be reached by calculation, rather than a jump |
| 196 | * table (which would be bigger than the code). |
| 197 | * |
| 198 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 199 | * so they can't be called from C directly. |
| 200 | */ |
| 201 | |
| 202 | struct fastop; |
| 203 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 204 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 205 | u64 flags : 56; |
| 206 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 207 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 208 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 209 | const struct opcode *group; |
| 210 | const struct group_dual *gdual; |
| 211 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 212 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 213 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 214 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 215 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | struct group_dual { |
| 219 | struct opcode mod012[8]; |
| 220 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 221 | }; |
| 222 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 223 | struct gprefix { |
| 224 | struct opcode pfx_no; |
| 225 | struct opcode pfx_66; |
| 226 | struct opcode pfx_f2; |
| 227 | struct opcode pfx_f3; |
| 228 | }; |
| 229 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 230 | struct escape { |
| 231 | struct opcode op[8]; |
| 232 | struct opcode high[64]; |
| 233 | }; |
| 234 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 235 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 236 | #define EFLG_ID (1<<21) |
| 237 | #define EFLG_VIP (1<<20) |
| 238 | #define EFLG_VIF (1<<19) |
| 239 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 240 | #define EFLG_VM (1<<17) |
| 241 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 242 | #define EFLG_IOPL (3<<12) |
| 243 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | #define EFLG_OF (1<<11) |
| 245 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 246 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 247 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 248 | #define EFLG_SF (1<<7) |
| 249 | #define EFLG_ZF (1<<6) |
| 250 | #define EFLG_AF (1<<4) |
| 251 | #define EFLG_PF (1<<2) |
| 252 | #define EFLG_CF (1<<0) |
| 253 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 254 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 255 | #define EFLG_RESERVED_ONE_MASK 2 |
| 256 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 257 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 258 | { |
| 259 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 260 | ctxt->regs_valid |= 1 << nr; |
| 261 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 262 | } |
| 263 | return ctxt->_regs[nr]; |
| 264 | } |
| 265 | |
| 266 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 267 | { |
| 268 | ctxt->regs_valid |= 1 << nr; |
| 269 | ctxt->regs_dirty |= 1 << nr; |
| 270 | return &ctxt->_regs[nr]; |
| 271 | } |
| 272 | |
| 273 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 274 | { |
| 275 | reg_read(ctxt, nr); |
| 276 | return reg_write(ctxt, nr); |
| 277 | } |
| 278 | |
| 279 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 280 | { |
| 281 | unsigned reg; |
| 282 | |
| 283 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 284 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 285 | } |
| 286 | |
| 287 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 288 | { |
| 289 | ctxt->regs_dirty = 0; |
| 290 | ctxt->regs_valid = 0; |
| 291 | } |
| 292 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 293 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | * These EFLAGS bits are restored from saved value during emulation, and |
| 295 | * any changes are written back to the saved value after emulation. |
| 296 | */ |
| 297 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 298 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 299 | #ifdef CONFIG_X86_64 |
| 300 | #define ON64(x) x |
| 301 | #else |
| 302 | #define ON64(x) |
| 303 | #endif |
| 304 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 305 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 306 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 307 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 308 | #define FOP_RET "ret \n\t" |
| 309 | |
| 310 | #define FOP_START(op) \ |
| 311 | extern void em_##op(struct fastop *fake); \ |
| 312 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 313 | ".global em_" #op " \n\t" \ |
| 314 | FOP_ALIGN \ |
| 315 | "em_" #op ": \n\t" |
| 316 | |
| 317 | #define FOP_END \ |
| 318 | ".popsection") |
| 319 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 320 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 321 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 322 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 323 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 324 | |
| 325 | #define FOP1EEX(op, dst) \ |
| 326 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 327 | |
| 328 | #define FASTOP1(op) \ |
| 329 | FOP_START(op) \ |
| 330 | FOP1E(op##b, al) \ |
| 331 | FOP1E(op##w, ax) \ |
| 332 | FOP1E(op##l, eax) \ |
| 333 | ON64(FOP1E(op##q, rax)) \ |
| 334 | FOP_END |
| 335 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 336 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 337 | #define FASTOP1SRC2(op, name) \ |
| 338 | FOP_START(name) \ |
| 339 | FOP1E(op, cl) \ |
| 340 | FOP1E(op, cx) \ |
| 341 | FOP1E(op, ecx) \ |
| 342 | ON64(FOP1E(op, rcx)) \ |
| 343 | FOP_END |
| 344 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 345 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 346 | #define FASTOP1SRC2EX(op, name) \ |
| 347 | FOP_START(name) \ |
| 348 | FOP1EEX(op, cl) \ |
| 349 | FOP1EEX(op, cx) \ |
| 350 | FOP1EEX(op, ecx) \ |
| 351 | ON64(FOP1EEX(op, rcx)) \ |
| 352 | FOP_END |
| 353 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 354 | #define FOP2E(op, dst, src) \ |
| 355 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 356 | |
| 357 | #define FASTOP2(op) \ |
| 358 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 359 | FOP2E(op##b, al, dl) \ |
| 360 | FOP2E(op##w, ax, dx) \ |
| 361 | FOP2E(op##l, eax, edx) \ |
| 362 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 363 | FOP_END |
| 364 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 365 | /* 2 operand, word only */ |
| 366 | #define FASTOP2W(op) \ |
| 367 | FOP_START(op) \ |
| 368 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 369 | FOP2E(op##w, ax, dx) \ |
| 370 | FOP2E(op##l, eax, edx) \ |
| 371 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 372 | FOP_END |
| 373 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 374 | /* 2 operand, src is CL */ |
| 375 | #define FASTOP2CL(op) \ |
| 376 | FOP_START(op) \ |
| 377 | FOP2E(op##b, al, cl) \ |
| 378 | FOP2E(op##w, ax, cl) \ |
| 379 | FOP2E(op##l, eax, cl) \ |
| 380 | ON64(FOP2E(op##q, rax, cl)) \ |
| 381 | FOP_END |
| 382 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 383 | /* 2 operand, src and dest are reversed */ |
| 384 | #define FASTOP2R(op, name) \ |
| 385 | FOP_START(name) \ |
| 386 | FOP2E(op##b, dl, al) \ |
| 387 | FOP2E(op##w, dx, ax) \ |
| 388 | FOP2E(op##l, edx, eax) \ |
| 389 | ON64(FOP2E(op##q, rdx, rax)) \ |
| 390 | FOP_END |
| 391 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 392 | #define FOP3E(op, dst, src, src2) \ |
| 393 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 394 | |
| 395 | /* 3-operand, word-only, src2=cl */ |
| 396 | #define FASTOP3WCL(op) \ |
| 397 | FOP_START(op) \ |
| 398 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 399 | FOP3E(op##w, ax, dx, cl) \ |
| 400 | FOP3E(op##l, eax, edx, cl) \ |
| 401 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 402 | FOP_END |
| 403 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 404 | /* Special case for SETcc - 1 instruction per cc */ |
| 405 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 406 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 407 | asm(".global kvm_fastop_exception \n" |
| 408 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 409 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 410 | FOP_START(setcc) |
| 411 | FOP_SETCC(seto) |
| 412 | FOP_SETCC(setno) |
| 413 | FOP_SETCC(setc) |
| 414 | FOP_SETCC(setnc) |
| 415 | FOP_SETCC(setz) |
| 416 | FOP_SETCC(setnz) |
| 417 | FOP_SETCC(setbe) |
| 418 | FOP_SETCC(setnbe) |
| 419 | FOP_SETCC(sets) |
| 420 | FOP_SETCC(setns) |
| 421 | FOP_SETCC(setp) |
| 422 | FOP_SETCC(setnp) |
| 423 | FOP_SETCC(setl) |
| 424 | FOP_SETCC(setnl) |
| 425 | FOP_SETCC(setle) |
| 426 | FOP_SETCC(setnle) |
| 427 | FOP_END; |
| 428 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 429 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 430 | FOP_END; |
| 431 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 432 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 433 | enum x86_intercept intercept, |
| 434 | enum x86_intercept_stage stage) |
| 435 | { |
| 436 | struct x86_instruction_info info = { |
| 437 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 438 | .rep_prefix = ctxt->rep_prefix, |
| 439 | .modrm_mod = ctxt->modrm_mod, |
| 440 | .modrm_reg = ctxt->modrm_reg, |
| 441 | .modrm_rm = ctxt->modrm_rm, |
| 442 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 443 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 444 | .src_bytes = ctxt->src.bytes, |
| 445 | .dst_bytes = ctxt->dst.bytes, |
| 446 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 447 | .next_rip = ctxt->eip, |
| 448 | }; |
| 449 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 450 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 453 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 454 | { |
| 455 | *dest = (*dest & ~mask) | (src & mask); |
| 456 | } |
| 457 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 458 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 459 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 460 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 463 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 464 | { |
| 465 | u16 sel; |
| 466 | struct desc_struct ss; |
| 467 | |
| 468 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 469 | return ~0UL; |
| 470 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 471 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 472 | } |
| 473 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 474 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 475 | { |
| 476 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 477 | } |
| 478 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 479 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 480 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 481 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 482 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 483 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 484 | return reg; |
| 485 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 486 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 490 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 491 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 492 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 495 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 496 | { |
| 497 | assign_masked(reg, *reg + inc, mask); |
| 498 | } |
| 499 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 500 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 501 | register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 502 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 503 | ulong mask; |
| 504 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 505 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 506 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 507 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 508 | mask = ad_mask(ctxt); |
| 509 | masked_increment(reg, mask, inc); |
| 510 | } |
| 511 | |
| 512 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 513 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 514 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 515 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 516 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 517 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 518 | { |
| 519 | u32 limit = get_desc_limit(desc); |
| 520 | |
| 521 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 522 | } |
| 523 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 524 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 525 | { |
| 526 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 527 | return 0; |
| 528 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 529 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 530 | } |
| 531 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 532 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 533 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 534 | { |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 535 | WARN_ON(vec > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 536 | ctxt->exception.vector = vec; |
| 537 | ctxt->exception.error_code = error; |
| 538 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 539 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 540 | } |
| 541 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 542 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 543 | { |
| 544 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 545 | } |
| 546 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 547 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 548 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 549 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 550 | } |
| 551 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 552 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 553 | { |
| 554 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 555 | } |
| 556 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 557 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 558 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 559 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 560 | } |
| 561 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 562 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 563 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 564 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 565 | } |
| 566 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 567 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 568 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 569 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 570 | } |
| 571 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 572 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 573 | { |
| 574 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 575 | } |
| 576 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 577 | static inline int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst, |
| 578 | int cs_l) |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 579 | { |
| 580 | switch (ctxt->op_bytes) { |
| 581 | case 2: |
| 582 | ctxt->_eip = (u16)dst; |
| 583 | break; |
| 584 | case 4: |
| 585 | ctxt->_eip = (u32)dst; |
| 586 | break; |
Nadav Amit | cd9b8e2c | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 587 | #ifdef CONFIG_X86_64 |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 588 | case 8: |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 589 | if ((cs_l && is_noncanonical_address(dst)) || |
Nadav Amit | cd9b8e2c | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 590 | (!cs_l && (dst >> 32) != 0)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 591 | return emulate_gp(ctxt, 0); |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 592 | ctxt->_eip = dst; |
| 593 | break; |
Nadav Amit | cd9b8e2c | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 594 | #endif |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 595 | default: |
| 596 | WARN(1, "unsupported eip assignment size\n"); |
| 597 | } |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 598 | return X86EMUL_CONTINUE; |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 599 | } |
| 600 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 601 | static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst) |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 602 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 603 | return assign_eip_far(ctxt, dst, ctxt->mode == X86EMUL_MODE_PROT64); |
| 604 | } |
| 605 | |
| 606 | static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
| 607 | { |
| 608 | return assign_eip_near(ctxt, ctxt->_eip + rel); |
Nadav Amit | 05c83ec | 2014-09-18 22:39:37 +0300 | [diff] [blame] | 609 | } |
| 610 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 611 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 612 | { |
| 613 | u16 selector; |
| 614 | struct desc_struct desc; |
| 615 | |
| 616 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 617 | return selector; |
| 618 | } |
| 619 | |
| 620 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 621 | unsigned seg) |
| 622 | { |
| 623 | u16 dummy; |
| 624 | u32 base3; |
| 625 | struct desc_struct desc; |
| 626 | |
| 627 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 628 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 629 | } |
| 630 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 631 | /* |
| 632 | * x86 defines three classes of vector instructions: explicitly |
| 633 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 634 | * depending on whether they're AVX encoded or not. |
| 635 | * |
| 636 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 637 | * subject to the same check. |
| 638 | */ |
| 639 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 640 | { |
| 641 | if (likely(size < 16)) |
| 642 | return false; |
| 643 | |
| 644 | if (ctxt->d & Aligned) |
| 645 | return true; |
| 646 | else if (ctxt->d & Unaligned) |
| 647 | return false; |
| 648 | else if (ctxt->d & Avx) |
| 649 | return false; |
| 650 | else |
| 651 | return true; |
| 652 | } |
| 653 | |
Paolo Bonzini | d09155d | 2014-10-27 14:54:44 +0100 | [diff] [blame] | 654 | static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt, |
| 655 | struct segmented_address addr, |
| 656 | unsigned *max_size, unsigned size, |
| 657 | bool write, bool fetch, |
| 658 | ulong *linear) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 659 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 660 | struct desc_struct desc; |
| 661 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 662 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 663 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 664 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 665 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 666 | |
Nadav Amit | 518547b | 2014-09-30 20:49:19 +0300 | [diff] [blame] | 667 | la = seg_base(ctxt, addr.seg) + |
| 668 | (fetch || ctxt->ad_bytes == 8 ? addr.ea : (u32)addr.ea); |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 669 | *max_size = 0; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 670 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 671 | case X86EMUL_MODE_PROT64: |
Nadav Amit | 4be4de7 | 2014-09-18 22:39:40 +0300 | [diff] [blame] | 672 | if (is_noncanonical_address(la)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 673 | return emulate_gp(ctxt, 0); |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 674 | |
| 675 | *max_size = min_t(u64, ~0u, (1ull << 48) - la); |
| 676 | if (size > *max_size) |
| 677 | goto bad; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 678 | break; |
| 679 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 680 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 681 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 682 | if (!usable) |
| 683 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 684 | /* code segment in protected mode or read-only data segment */ |
| 685 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 686 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 687 | goto bad; |
| 688 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 689 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 690 | goto bad; |
| 691 | lim = desc_limit_scaled(&desc); |
Nadav Amit | 10e38fc | 2014-06-18 17:19:34 +0300 | [diff] [blame] | 692 | if ((ctxt->mode == X86EMUL_MODE_REAL) && !fetch && |
| 693 | (ctxt->d & NoBigReal)) { |
| 694 | /* la is between zero and 0xffff */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 695 | if (la > 0xffff) |
Nadav Amit | 10e38fc | 2014-06-18 17:19:34 +0300 | [diff] [blame] | 696 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 697 | *max_size = 0x10000 - la; |
Nadav Amit | 10e38fc | 2014-06-18 17:19:34 +0300 | [diff] [blame] | 698 | } else if ((desc.type & 8) || !(desc.type & 4)) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 699 | /* expand-up segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 700 | if (addr.ea > lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 701 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 702 | *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 703 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 704 | /* expand-down segment */ |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 705 | if (addr.ea <= lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 706 | goto bad; |
| 707 | lim = desc.d ? 0xffffffff : 0xffff; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 708 | if (addr.ea > lim) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 709 | goto bad; |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 710 | *max_size = min_t(u64, ~0u, (u64)lim + 1 - addr.ea); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 711 | } |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 712 | if (size > *max_size) |
| 713 | goto bad; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 714 | cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | c49c759 | 2014-10-03 01:10:03 +0300 | [diff] [blame] | 715 | if (!fetch) { |
| 716 | /* data segment or readable code segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 717 | if (cpl > desc.dpl) |
| 718 | goto bad; |
| 719 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 720 | /* nonconforming code segment */ |
| 721 | if (cpl != desc.dpl) |
| 722 | goto bad; |
| 723 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 724 | /* conforming code segment */ |
| 725 | if (cpl < desc.dpl) |
| 726 | goto bad; |
| 727 | } |
| 728 | break; |
| 729 | } |
Nadav Amit | 518547b | 2014-09-30 20:49:19 +0300 | [diff] [blame] | 730 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 731 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 732 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 733 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 734 | *linear = la; |
| 735 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 736 | bad: |
| 737 | if (addr.seg == VCPU_SREG_SS) |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 738 | return emulate_ss(ctxt, 0); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 739 | else |
Paolo Bonzini | 3606189 | 2014-10-27 14:40:49 +0100 | [diff] [blame] | 740 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 741 | } |
| 742 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 743 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 744 | struct segmented_address addr, |
| 745 | unsigned size, bool write, |
| 746 | ulong *linear) |
| 747 | { |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 748 | unsigned max_size; |
| 749 | return __linearize(ctxt, addr, &max_size, size, write, false, linear); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 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 | */ |
| 790 | rc = __linearize(ctxt, addr, &max_size, 0, false, true, &linear); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 791 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 792 | return rc; |
| 793 | |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 794 | size = min_t(unsigned, 15UL ^ cur_size, max_size); |
Paolo Bonzini | 719d5a9 | 2014-06-19 11:37:06 +0200 | [diff] [blame] | 795 | size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear)); |
Paolo Bonzini | 5cfc7e0 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * One instruction can only straddle two pages, |
| 799 | * and one has been loaded at the beginning of |
| 800 | * x86_decode_insn. So, if not enough bytes |
| 801 | * still, we must have hit the 15-byte boundary. |
| 802 | */ |
| 803 | if (unlikely(size < op_size)) |
Paolo Bonzini | fd56e15 | 2014-10-27 14:40:39 +0100 | [diff] [blame] | 804 | return emulate_gp(ctxt, 0); |
| 805 | |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 806 | rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 807 | size, &ctxt->exception); |
| 808 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 809 | return rc; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 810 | ctxt->fetch.end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 811 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 814 | static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, |
| 815 | unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 816 | { |
Nadav Amit | 08da44a | 2014-10-03 01:10:04 +0300 | [diff] [blame] | 817 | unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr; |
| 818 | |
| 819 | if (unlikely(done_size < size)) |
| 820 | return __do_insn_fetch_bytes(ctxt, size - done_size); |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 821 | else |
| 822 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 825 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 826 | #define insn_fetch(_type, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 827 | ({ _type _x; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 828 | \ |
| 829 | rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 830 | if (rc != X86EMUL_CONTINUE) \ |
| 831 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 832 | ctxt->_eip += sizeof(_type); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 833 | _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \ |
| 834 | ctxt->fetch.ptr += sizeof(_type); \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 835 | _x; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 836 | }) |
| 837 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 838 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 839 | ({ \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 840 | rc = do_insn_fetch_bytes(_ctxt, _size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 841 | if (rc != X86EMUL_CONTINUE) \ |
| 842 | goto done; \ |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 843 | ctxt->_eip += (_size); \ |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 844 | memcpy(_arr, ctxt->fetch.ptr, _size); \ |
| 845 | ctxt->fetch.ptr += (_size); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 846 | }) |
| 847 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 848 | /* |
| 849 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 850 | * pointer into the block that addresses the relevant register. |
| 851 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 852 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 853 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 854 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 855 | { |
| 856 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 857 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 858 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 859 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 860 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 861 | else |
| 862 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 863 | return p; |
| 864 | } |
| 865 | |
| 866 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 867 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 868 | u16 *size, unsigned long *address, int op_bytes) |
| 869 | { |
| 870 | int rc; |
| 871 | |
| 872 | if (op_bytes == 2) |
| 873 | op_bytes = 3; |
| 874 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 875 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 876 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 877 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 878 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 879 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 880 | return rc; |
| 881 | } |
| 882 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 883 | FASTOP2(add); |
| 884 | FASTOP2(or); |
| 885 | FASTOP2(adc); |
| 886 | FASTOP2(sbb); |
| 887 | FASTOP2(and); |
| 888 | FASTOP2(sub); |
| 889 | FASTOP2(xor); |
| 890 | FASTOP2(cmp); |
| 891 | FASTOP2(test); |
| 892 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 893 | FASTOP1SRC2(mul, mul_ex); |
| 894 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 895 | FASTOP1SRC2EX(div, div_ex); |
| 896 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 897 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 898 | FASTOP3WCL(shld); |
| 899 | FASTOP3WCL(shrd); |
| 900 | |
| 901 | FASTOP2W(imul); |
| 902 | |
| 903 | FASTOP1(not); |
| 904 | FASTOP1(neg); |
| 905 | FASTOP1(inc); |
| 906 | FASTOP1(dec); |
| 907 | |
| 908 | FASTOP2CL(rol); |
| 909 | FASTOP2CL(ror); |
| 910 | FASTOP2CL(rcl); |
| 911 | FASTOP2CL(rcr); |
| 912 | FASTOP2CL(shl); |
| 913 | FASTOP2CL(shr); |
| 914 | FASTOP2CL(sar); |
| 915 | |
| 916 | FASTOP2W(bsf); |
| 917 | FASTOP2W(bsr); |
| 918 | FASTOP2W(bt); |
| 919 | FASTOP2W(bts); |
| 920 | FASTOP2W(btr); |
| 921 | FASTOP2W(btc); |
| 922 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 923 | FASTOP2(xadd); |
| 924 | |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 925 | FASTOP2R(cmp, cmp_r); |
| 926 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 927 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 928 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 929 | u8 rc; |
| 930 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 931 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 932 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 933 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 934 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 935 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 936 | } |
| 937 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 938 | static void fetch_register_operand(struct operand *op) |
| 939 | { |
| 940 | switch (op->bytes) { |
| 941 | case 1: |
| 942 | op->val = *(u8 *)op->addr.reg; |
| 943 | break; |
| 944 | case 2: |
| 945 | op->val = *(u16 *)op->addr.reg; |
| 946 | break; |
| 947 | case 4: |
| 948 | op->val = *(u32 *)op->addr.reg; |
| 949 | break; |
| 950 | case 8: |
| 951 | op->val = *(u64 *)op->addr.reg; |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 956 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 957 | { |
| 958 | ctxt->ops->get_fpu(ctxt); |
| 959 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 960 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 961 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 962 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 963 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 964 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 965 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 966 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 967 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 968 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 969 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 970 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 971 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 972 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 973 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 974 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 975 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 976 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 977 | #endif |
| 978 | default: BUG(); |
| 979 | } |
| 980 | ctxt->ops->put_fpu(ctxt); |
| 981 | } |
| 982 | |
| 983 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 984 | int reg) |
| 985 | { |
| 986 | ctxt->ops->get_fpu(ctxt); |
| 987 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 988 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 989 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 990 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 991 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 992 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 993 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 994 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 995 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 996 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 997 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 998 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 999 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1000 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1001 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1002 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1003 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1004 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1005 | #endif |
| 1006 | default: BUG(); |
| 1007 | } |
| 1008 | ctxt->ops->put_fpu(ctxt); |
| 1009 | } |
| 1010 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1011 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1012 | { |
| 1013 | ctxt->ops->get_fpu(ctxt); |
| 1014 | switch (reg) { |
| 1015 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1016 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1017 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1018 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1019 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1020 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1021 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1022 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1023 | default: BUG(); |
| 1024 | } |
| 1025 | ctxt->ops->put_fpu(ctxt); |
| 1026 | } |
| 1027 | |
| 1028 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1029 | { |
| 1030 | ctxt->ops->get_fpu(ctxt); |
| 1031 | switch (reg) { |
| 1032 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1033 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1034 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1035 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1036 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1037 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1038 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1039 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1040 | default: BUG(); |
| 1041 | } |
| 1042 | ctxt->ops->put_fpu(ctxt); |
| 1043 | } |
| 1044 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1045 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1046 | { |
| 1047 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1048 | return emulate_nm(ctxt); |
| 1049 | |
| 1050 | ctxt->ops->get_fpu(ctxt); |
| 1051 | asm volatile("fninit"); |
| 1052 | ctxt->ops->put_fpu(ctxt); |
| 1053 | return X86EMUL_CONTINUE; |
| 1054 | } |
| 1055 | |
| 1056 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1057 | { |
| 1058 | u16 fcw; |
| 1059 | |
| 1060 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1061 | return emulate_nm(ctxt); |
| 1062 | |
| 1063 | ctxt->ops->get_fpu(ctxt); |
| 1064 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1065 | ctxt->ops->put_fpu(ctxt); |
| 1066 | |
| 1067 | /* force 2 byte destination */ |
| 1068 | ctxt->dst.bytes = 2; |
| 1069 | ctxt->dst.val = fcw; |
| 1070 | |
| 1071 | return X86EMUL_CONTINUE; |
| 1072 | } |
| 1073 | |
| 1074 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1075 | { |
| 1076 | u16 fsw; |
| 1077 | |
| 1078 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1079 | return emulate_nm(ctxt); |
| 1080 | |
| 1081 | ctxt->ops->get_fpu(ctxt); |
| 1082 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1083 | ctxt->ops->put_fpu(ctxt); |
| 1084 | |
| 1085 | /* force 2 byte destination */ |
| 1086 | ctxt->dst.bytes = 2; |
| 1087 | ctxt->dst.val = fsw; |
| 1088 | |
| 1089 | return X86EMUL_CONTINUE; |
| 1090 | } |
| 1091 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1092 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1093 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1094 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1095 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1096 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1097 | if (!(ctxt->d & ModRM)) |
| 1098 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1099 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1100 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1101 | op->type = OP_XMM; |
| 1102 | op->bytes = 16; |
| 1103 | op->addr.xmm = reg; |
| 1104 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1105 | return; |
| 1106 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1107 | if (ctxt->d & Mmx) { |
| 1108 | reg &= 7; |
| 1109 | op->type = OP_MM; |
| 1110 | op->bytes = 8; |
| 1111 | op->addr.mm = reg; |
| 1112 | return; |
| 1113 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1114 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1115 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1116 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1117 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1118 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1119 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1120 | op->orig_val = op->val; |
| 1121 | } |
| 1122 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1123 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1124 | { |
| 1125 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1126 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1127 | } |
| 1128 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1129 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1130 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1131 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1132 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1133 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1134 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1135 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1136 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1137 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1138 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1139 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1140 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1141 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1142 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1143 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1144 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1145 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1146 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1147 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1148 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1149 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1150 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1151 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1152 | op->type = OP_XMM; |
| 1153 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1154 | op->addr.xmm = ctxt->modrm_rm; |
| 1155 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1156 | return rc; |
| 1157 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1158 | if (ctxt->d & Mmx) { |
| 1159 | op->type = OP_MM; |
| 1160 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1161 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1162 | return rc; |
| 1163 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1164 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1165 | return rc; |
| 1166 | } |
| 1167 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1168 | op->type = OP_MEM; |
| 1169 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1170 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1171 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1172 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1173 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1174 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1175 | |
| 1176 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1177 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1178 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1179 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1180 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1181 | break; |
| 1182 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1183 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1184 | break; |
| 1185 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1186 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1187 | break; |
| 1188 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1189 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1190 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1191 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1192 | break; |
| 1193 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1194 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1195 | break; |
| 1196 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1197 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1198 | break; |
| 1199 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1200 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1201 | break; |
| 1202 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1203 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1204 | break; |
| 1205 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1206 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1207 | break; |
| 1208 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1209 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1210 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1211 | break; |
| 1212 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1213 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1214 | break; |
| 1215 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1216 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1217 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1218 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1219 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1220 | } else { |
| 1221 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1222 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1223 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1224 | index_reg |= (sib >> 3) & 7; |
| 1225 | base_reg |= sib & 7; |
| 1226 | scale = sib >> 6; |
| 1227 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1228 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1229 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1230 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1231 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1232 | adjust_modrm_seg(ctxt, base_reg); |
| 1233 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1234 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1235 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1236 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Nadav Amit | 5b38ab8 | 2014-11-02 11:54:41 +0200 | [diff] [blame] | 1237 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1238 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1239 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1240 | } else { |
| 1241 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1242 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1243 | adjust_modrm_seg(ctxt, base_reg); |
| 1244 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1245 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1246 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1247 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1248 | break; |
| 1249 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1250 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1254 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1255 | if (ctxt->ad_bytes != 8) |
| 1256 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1257 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1258 | done: |
| 1259 | return rc; |
| 1260 | } |
| 1261 | |
| 1262 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1263 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1264 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1265 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1266 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1267 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1268 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1269 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1270 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1271 | break; |
| 1272 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1273 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1274 | break; |
| 1275 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1276 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | } |
| 1279 | done: |
| 1280 | return rc; |
| 1281 | } |
| 1282 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1283 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1284 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1285 | long sv = 0, mask; |
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->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1288 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1289 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1290 | if (ctxt->src.bytes == 2) |
| 1291 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1292 | else if (ctxt->src.bytes == 4) |
| 1293 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1294 | else |
| 1295 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1296 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1297 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1298 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1299 | |
| 1300 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1301 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1304 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1305 | unsigned long addr, void *dest, unsigned size) |
| 1306 | { |
| 1307 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1308 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1309 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1310 | if (mc->pos < mc->end) |
| 1311 | goto read_cached; |
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 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1314 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1315 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1316 | &ctxt->exception); |
| 1317 | if (rc != X86EMUL_CONTINUE) |
| 1318 | return rc; |
| 1319 | |
| 1320 | mc->end += size; |
| 1321 | |
| 1322 | read_cached: |
| 1323 | memcpy(dest, mc->data + mc->pos, size); |
| 1324 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1325 | return X86EMUL_CONTINUE; |
| 1326 | } |
| 1327 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1328 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1329 | struct segmented_address addr, |
| 1330 | void *data, |
| 1331 | unsigned size) |
| 1332 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1333 | int rc; |
| 1334 | ulong linear; |
| 1335 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1336 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1337 | if (rc != X86EMUL_CONTINUE) |
| 1338 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1339 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1343 | struct segmented_address addr, |
| 1344 | const void *data, |
| 1345 | unsigned size) |
| 1346 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1347 | int rc; |
| 1348 | ulong linear; |
| 1349 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1350 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1351 | if (rc != X86EMUL_CONTINUE) |
| 1352 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1353 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1354 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1358 | struct segmented_address addr, |
| 1359 | const void *orig_data, const void *data, |
| 1360 | unsigned size) |
| 1361 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1362 | int rc; |
| 1363 | ulong linear; |
| 1364 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1365 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1366 | if (rc != X86EMUL_CONTINUE) |
| 1367 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1368 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1369 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1372 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1373 | unsigned int size, unsigned short port, |
| 1374 | void *dest) |
| 1375 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1376 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1379 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1380 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1381 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1382 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1383 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1384 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Mark Rustad | b55a814 | 2014-07-25 06:27:05 -0700 | [diff] [blame] | 1385 | n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1386 | if (n == 0) |
| 1387 | n = 1; |
| 1388 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1389 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1390 | return 0; |
| 1391 | rc->end = n * size; |
| 1392 | } |
| 1393 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1394 | if (ctxt->rep_prefix && (ctxt->d & String) && |
| 1395 | !(ctxt->eflags & EFLG_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1396 | ctxt->dst.data = rc->data + rc->pos; |
| 1397 | ctxt->dst.type = OP_MEM_STR; |
| 1398 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1399 | rc->pos = rc->end; |
| 1400 | } else { |
| 1401 | memcpy(dest, rc->data + rc->pos, size); |
| 1402 | rc->pos += size; |
| 1403 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1404 | return 1; |
| 1405 | } |
| 1406 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1407 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1408 | u16 index, struct desc_struct *desc) |
| 1409 | { |
| 1410 | struct desc_ptr dt; |
| 1411 | ulong addr; |
| 1412 | |
| 1413 | ctxt->ops->get_idt(ctxt, &dt); |
| 1414 | |
| 1415 | if (dt.size < index * 8 + 7) |
| 1416 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1417 | |
| 1418 | addr = dt.address + index * 8; |
| 1419 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1420 | &ctxt->exception); |
| 1421 | } |
| 1422 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1423 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1424 | u16 selector, struct desc_ptr *dt) |
| 1425 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1426 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1427 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1428 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1429 | if (selector & 1 << 2) { |
| 1430 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1431 | u16 sel; |
| 1432 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1433 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1434 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1435 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1436 | return; |
| 1437 | |
| 1438 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1439 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1440 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1441 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* allowed just for 8 bytes segments */ |
| 1445 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1446 | u16 selector, struct desc_struct *desc, |
| 1447 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1448 | { |
| 1449 | struct desc_ptr dt; |
| 1450 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1451 | ulong addr; |
| 1452 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1453 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1454 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1455 | if (dt.size < index * 8 + 7) |
| 1456 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1457 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1458 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1459 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1460 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* allowed just for 8 bytes segments */ |
| 1464 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1465 | u16 selector, struct desc_struct *desc) |
| 1466 | { |
| 1467 | struct desc_ptr dt; |
| 1468 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1469 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1470 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1471 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1472 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1473 | if (dt.size < index * 8 + 7) |
| 1474 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1475 | |
| 1476 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1477 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1478 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1481 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1482 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1483 | u16 selector, int seg, u8 cpl, |
| 1484 | bool in_task_switch, |
| 1485 | struct desc_struct *desc) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1486 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1487 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1488 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1489 | unsigned err_vec = GP_VECTOR; |
| 1490 | u32 err_code = 0; |
| 1491 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1492 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1493 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1494 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1495 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1496 | |
| 1497 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1498 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1499 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1500 | /* set real mode segment descriptor (keep limit etc. for |
| 1501 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1502 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1503 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1504 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1505 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1506 | /* VM86 needs a clean new segment descriptor */ |
| 1507 | set_desc_base(&seg_desc, selector << 4); |
| 1508 | set_desc_limit(&seg_desc, 0xffff); |
| 1509 | seg_desc.type = 3; |
| 1510 | seg_desc.p = 1; |
| 1511 | seg_desc.s = 1; |
| 1512 | seg_desc.dpl = 3; |
| 1513 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1516 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1517 | |
| 1518 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1519 | if ((seg == VCPU_SREG_CS |
| 1520 | || (seg == VCPU_SREG_SS |
| 1521 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1522 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1523 | && null_selector) |
| 1524 | goto exception; |
| 1525 | |
| 1526 | /* TR should be in GDT only */ |
| 1527 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1528 | goto exception; |
| 1529 | |
| 1530 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1531 | goto load; |
| 1532 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1533 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1534 | if (ret != X86EMUL_CONTINUE) |
| 1535 | return ret; |
| 1536 | |
| 1537 | err_code = selector & 0xfffc; |
Paolo Bonzini | 15fc075 | 2014-08-18 13:17:00 +0200 | [diff] [blame] | 1538 | err_vec = in_task_switch ? TS_VECTOR : GP_VECTOR; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1539 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1540 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1541 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1542 | goto exception; |
| 1543 | |
| 1544 | if (!seg_desc.p) { |
| 1545 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1546 | goto exception; |
| 1547 | } |
| 1548 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1549 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1550 | |
| 1551 | switch (seg) { |
| 1552 | case VCPU_SREG_SS: |
| 1553 | /* |
| 1554 | * segment is not a writable data segment or segment |
| 1555 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1556 | */ |
| 1557 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1558 | goto exception; |
| 1559 | break; |
| 1560 | case VCPU_SREG_CS: |
| 1561 | if (!(seg_desc.type & 8)) |
| 1562 | goto exception; |
| 1563 | |
| 1564 | if (seg_desc.type & 4) { |
| 1565 | /* conforming */ |
| 1566 | if (dpl > cpl) |
| 1567 | goto exception; |
| 1568 | } else { |
| 1569 | /* nonconforming */ |
| 1570 | if (rpl > cpl || dpl != cpl) |
| 1571 | goto exception; |
| 1572 | } |
Nadav Amit | 040c8dc | 2014-09-18 22:39:43 +0300 | [diff] [blame] | 1573 | /* in long-mode d/b must be clear if l is set */ |
| 1574 | if (seg_desc.d && seg_desc.l) { |
| 1575 | u64 efer = 0; |
| 1576 | |
| 1577 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 1578 | if (efer & EFER_LMA) |
| 1579 | goto exception; |
| 1580 | } |
| 1581 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1582 | /* CS(RPL) <- CPL */ |
| 1583 | selector = (selector & 0xfffc) | cpl; |
| 1584 | break; |
| 1585 | case VCPU_SREG_TR: |
| 1586 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1587 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1588 | old_desc = seg_desc; |
| 1589 | seg_desc.type |= 2; /* busy */ |
| 1590 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1591 | sizeof(seg_desc), &ctxt->exception); |
| 1592 | if (ret != X86EMUL_CONTINUE) |
| 1593 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1594 | break; |
| 1595 | case VCPU_SREG_LDTR: |
| 1596 | if (seg_desc.s || seg_desc.type != 2) |
| 1597 | goto exception; |
| 1598 | break; |
| 1599 | default: /* DS, ES, FS, or GS */ |
| 1600 | /* |
| 1601 | * segment is not a data or readable code segment or |
| 1602 | * ((segment is a data or nonconforming code segment) |
| 1603 | * and (both RPL and CPL > DPL)) |
| 1604 | */ |
| 1605 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1606 | (((seg_desc.type & 0xc) != 0xc) && |
| 1607 | (rpl > dpl && cpl > dpl))) |
| 1608 | goto exception; |
| 1609 | break; |
| 1610 | } |
| 1611 | |
| 1612 | if (seg_desc.s) { |
| 1613 | /* mark segment as accessed */ |
| 1614 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1615 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1616 | if (ret != X86EMUL_CONTINUE) |
| 1617 | return ret; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1618 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1619 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1620 | sizeof(base3), &ctxt->exception); |
| 1621 | if (ret != X86EMUL_CONTINUE) |
| 1622 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1623 | } |
| 1624 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1625 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1626 | if (desc) |
| 1627 | *desc = seg_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1628 | return X86EMUL_CONTINUE; |
| 1629 | exception: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 1630 | return emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1633 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1634 | u16 selector, int seg) |
| 1635 | { |
| 1636 | u8 cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 1637 | return __load_segment_descriptor(ctxt, selector, seg, cpl, false, NULL); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1638 | } |
| 1639 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1640 | static void write_register_operand(struct operand *op) |
| 1641 | { |
| 1642 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1643 | switch (op->bytes) { |
| 1644 | case 1: |
| 1645 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1646 | break; |
| 1647 | case 2: |
| 1648 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1649 | break; |
| 1650 | case 4: |
| 1651 | *op->addr.reg = (u32)op->val; |
| 1652 | break; /* 64b: zero-extend */ |
| 1653 | case 8: |
| 1654 | *op->addr.reg = op->val; |
| 1655 | break; |
| 1656 | } |
| 1657 | } |
| 1658 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1659 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1660 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1661 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1662 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1663 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1664 | break; |
| 1665 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1666 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1667 | return segmented_cmpxchg(ctxt, |
| 1668 | op->addr.mem, |
| 1669 | &op->orig_val, |
| 1670 | &op->val, |
| 1671 | op->bytes); |
| 1672 | else |
| 1673 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1674 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1675 | &op->val, |
| 1676 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1677 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1678 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1679 | return segmented_write(ctxt, |
| 1680 | op->addr.mem, |
| 1681 | op->data, |
| 1682 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1683 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1684 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1685 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1686 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1687 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1688 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1689 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1690 | case OP_NONE: |
| 1691 | /* no writeback */ |
| 1692 | break; |
| 1693 | default: |
| 1694 | break; |
| 1695 | } |
| 1696 | return X86EMUL_CONTINUE; |
| 1697 | } |
| 1698 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1699 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1700 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1701 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1702 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1703 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1704 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1705 | addr.seg = VCPU_SREG_SS; |
| 1706 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1707 | return segmented_write(ctxt, addr, data, bytes); |
| 1708 | } |
| 1709 | |
| 1710 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1711 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1712 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1713 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1714 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1717 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1718 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1719 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1720 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1721 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1722 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1723 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1724 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1725 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1726 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1727 | return rc; |
| 1728 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1729 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1730 | return rc; |
| 1731 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1732 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1733 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1734 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1735 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1736 | } |
| 1737 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1738 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1739 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1740 | { |
| 1741 | int rc; |
| 1742 | unsigned long val, change_mask; |
| 1743 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1744 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1745 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1746 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1747 | if (rc != X86EMUL_CONTINUE) |
| 1748 | return rc; |
| 1749 | |
| 1750 | 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] | 1751 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_AC | EFLG_ID; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1752 | |
| 1753 | switch(ctxt->mode) { |
| 1754 | case X86EMUL_MODE_PROT64: |
| 1755 | case X86EMUL_MODE_PROT32: |
| 1756 | case X86EMUL_MODE_PROT16: |
| 1757 | if (cpl == 0) |
| 1758 | change_mask |= EFLG_IOPL; |
| 1759 | if (cpl <= iopl) |
| 1760 | change_mask |= EFLG_IF; |
| 1761 | break; |
| 1762 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1763 | if (iopl < 3) |
| 1764 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1765 | change_mask |= EFLG_IF; |
| 1766 | break; |
| 1767 | default: /* real mode */ |
| 1768 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1769 | break; |
| 1770 | } |
| 1771 | |
| 1772 | *(unsigned long *)dest = |
| 1773 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1774 | |
| 1775 | return rc; |
| 1776 | } |
| 1777 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1778 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1779 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1780 | ctxt->dst.type = OP_REG; |
| 1781 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1782 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1783 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1784 | } |
| 1785 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1786 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1787 | { |
| 1788 | int rc; |
| 1789 | unsigned frame_size = ctxt->src.val; |
| 1790 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1791 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1792 | |
| 1793 | if (nesting_level) |
| 1794 | return X86EMUL_UNHANDLEABLE; |
| 1795 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1796 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1797 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1798 | if (rc != X86EMUL_CONTINUE) |
| 1799 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1800 | 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] | 1801 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1802 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1803 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1804 | stack_mask(ctxt)); |
| 1805 | return X86EMUL_CONTINUE; |
| 1806 | } |
| 1807 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1808 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1809 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1810 | 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] | 1811 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1812 | 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] | 1813 | } |
| 1814 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1815 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1816 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1817 | int seg = ctxt->src2.val; |
| 1818 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1819 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Nadav Amit | 0fcc207 | 2014-11-02 11:54:51 +0200 | [diff] [blame] | 1820 | if (ctxt->op_bytes == 4) { |
| 1821 | rsp_increment(ctxt, -2); |
| 1822 | ctxt->op_bytes = 2; |
| 1823 | } |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1824 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1825 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1826 | } |
| 1827 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1828 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1829 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1830 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1831 | unsigned long selector; |
| 1832 | int rc; |
| 1833 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1834 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1835 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1836 | return rc; |
| 1837 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1838 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1839 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 1840 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1841 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1842 | return rc; |
| 1843 | } |
| 1844 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1845 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1846 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1847 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1848 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1849 | int reg = VCPU_REGS_RAX; |
| 1850 | |
| 1851 | while (reg <= VCPU_REGS_RDI) { |
| 1852 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1853 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1854 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1855 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1856 | if (rc != X86EMUL_CONTINUE) |
| 1857 | return rc; |
| 1858 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1859 | ++reg; |
| 1860 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1861 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1862 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1865 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1866 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1867 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1868 | return em_push(ctxt); |
| 1869 | } |
| 1870 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1871 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1872 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1873 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1874 | int reg = VCPU_REGS_RDI; |
| 1875 | |
| 1876 | while (reg >= VCPU_REGS_RAX) { |
| 1877 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1878 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1879 | --reg; |
| 1880 | } |
| 1881 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1882 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1883 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1884 | break; |
| 1885 | --reg; |
| 1886 | } |
| 1887 | return rc; |
| 1888 | } |
| 1889 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1890 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1891 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1892 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1893 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1894 | struct desc_ptr dt; |
| 1895 | gva_t cs_addr; |
| 1896 | gva_t eip_addr; |
| 1897 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1898 | |
| 1899 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1900 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1901 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1902 | if (rc != X86EMUL_CONTINUE) |
| 1903 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1904 | |
| 1905 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1906 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1907 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1908 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1909 | if (rc != X86EMUL_CONTINUE) |
| 1910 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1911 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1912 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1913 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1914 | if (rc != X86EMUL_CONTINUE) |
| 1915 | return rc; |
| 1916 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1917 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1918 | |
| 1919 | eip_addr = dt.address + (irq << 2); |
| 1920 | cs_addr = dt.address + (irq << 2) + 2; |
| 1921 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1922 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1923 | if (rc != X86EMUL_CONTINUE) |
| 1924 | return rc; |
| 1925 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1926 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1927 | if (rc != X86EMUL_CONTINUE) |
| 1928 | return rc; |
| 1929 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1930 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1931 | if (rc != X86EMUL_CONTINUE) |
| 1932 | return rc; |
| 1933 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1934 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1935 | |
| 1936 | return rc; |
| 1937 | } |
| 1938 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1939 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1940 | { |
| 1941 | int rc; |
| 1942 | |
| 1943 | invalidate_registers(ctxt); |
| 1944 | rc = __emulate_int_real(ctxt, irq); |
| 1945 | if (rc == X86EMUL_CONTINUE) |
| 1946 | writeback_registers(ctxt); |
| 1947 | return rc; |
| 1948 | } |
| 1949 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1950 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1951 | { |
| 1952 | switch(ctxt->mode) { |
| 1953 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1954 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1955 | case X86EMUL_MODE_VM86: |
| 1956 | case X86EMUL_MODE_PROT16: |
| 1957 | case X86EMUL_MODE_PROT32: |
| 1958 | case X86EMUL_MODE_PROT64: |
| 1959 | default: |
| 1960 | /* Protected mode interrupts unimplemented yet */ |
| 1961 | return X86EMUL_UNHANDLEABLE; |
| 1962 | } |
| 1963 | } |
| 1964 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1965 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1966 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1967 | int rc = X86EMUL_CONTINUE; |
| 1968 | unsigned long temp_eip = 0; |
| 1969 | unsigned long temp_eflags = 0; |
| 1970 | unsigned long cs = 0; |
| 1971 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1972 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1973 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1974 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1975 | |
| 1976 | /* TODO: Add stack limit check */ |
| 1977 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1978 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1979 | |
| 1980 | if (rc != X86EMUL_CONTINUE) |
| 1981 | return rc; |
| 1982 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1983 | if (temp_eip & ~0xffff) |
| 1984 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1985 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1986 | rc = emulate_pop(ctxt, &cs, 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 | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1991 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1992 | |
| 1993 | if (rc != X86EMUL_CONTINUE) |
| 1994 | return rc; |
| 1995 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1996 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1997 | |
| 1998 | if (rc != X86EMUL_CONTINUE) |
| 1999 | return rc; |
| 2000 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2001 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2002 | |
| 2003 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2004 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2005 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2006 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2007 | ctxt->eflags &= ~0xffff; |
| 2008 | ctxt->eflags |= temp_eflags; |
| 2009 | } |
| 2010 | |
| 2011 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 2012 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 2013 | |
| 2014 | return rc; |
| 2015 | } |
| 2016 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2017 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2018 | { |
| 2019 | switch(ctxt->mode) { |
| 2020 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2021 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2022 | case X86EMUL_MODE_VM86: |
| 2023 | case X86EMUL_MODE_PROT16: |
| 2024 | case X86EMUL_MODE_PROT32: |
| 2025 | case X86EMUL_MODE_PROT64: |
| 2026 | default: |
| 2027 | /* iret from protected mode unimplemented yet */ |
| 2028 | return X86EMUL_UNHANDLEABLE; |
| 2029 | } |
| 2030 | } |
| 2031 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2032 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2033 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2034 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2035 | unsigned short sel, old_sel; |
| 2036 | struct desc_struct old_desc, new_desc; |
| 2037 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 2038 | u8 cpl = ctxt->ops->cpl(ctxt); |
| 2039 | |
| 2040 | /* Assignment of RIP may only fail in 64-bit mode */ |
| 2041 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2042 | ops->get_segment(ctxt, &old_sel, &old_desc, NULL, |
| 2043 | VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2044 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2045 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2046 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2047 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false, |
| 2048 | &new_desc); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2049 | if (rc != X86EMUL_CONTINUE) |
| 2050 | return rc; |
| 2051 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2052 | rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l); |
| 2053 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | cd9b8e2c | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2054 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2055 | /* assigning eip failed; restore the old cs */ |
| 2056 | ops->set_segment(ctxt, old_sel, &old_desc, 0, VCPU_SREG_CS); |
| 2057 | return rc; |
| 2058 | } |
| 2059 | return rc; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2060 | } |
| 2061 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2062 | static int em_jmp_abs(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2063 | { |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2064 | return assign_eip_near(ctxt, ctxt->src.val); |
| 2065 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2066 | |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 2067 | static int em_call_near_abs(struct x86_emulate_ctxt *ctxt) |
| 2068 | { |
| 2069 | int rc; |
| 2070 | long int old_eip; |
| 2071 | |
| 2072 | old_eip = ctxt->_eip; |
| 2073 | rc = assign_eip_near(ctxt, ctxt->src.val); |
| 2074 | if (rc != X86EMUL_CONTINUE) |
| 2075 | return rc; |
| 2076 | ctxt->src.val = old_eip; |
| 2077 | rc = em_push(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2078 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2081 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2082 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2083 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2084 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2085 | if (ctxt->dst.bytes == 16) |
| 2086 | return X86EMUL_UNHANDLEABLE; |
| 2087 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2088 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2089 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2090 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2091 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2092 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2093 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2094 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2095 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2096 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2097 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2098 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2099 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2102 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2103 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2104 | int rc; |
| 2105 | unsigned long eip; |
| 2106 | |
| 2107 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 2108 | if (rc != X86EMUL_CONTINUE) |
| 2109 | return rc; |
| 2110 | |
| 2111 | return assign_eip_near(ctxt, eip); |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2112 | } |
| 2113 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2114 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2115 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2116 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2117 | unsigned long eip, cs; |
| 2118 | u16 old_cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2119 | int cpl = ctxt->ops->cpl(ctxt); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2120 | struct desc_struct old_desc, new_desc; |
| 2121 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2122 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2123 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2124 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, |
| 2125 | VCPU_SREG_CS); |
| 2126 | |
| 2127 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2128 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2129 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2130 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2131 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2132 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2133 | /* Outer-privilege level return is not implemented */ |
| 2134 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2135 | return X86EMUL_UNHANDLEABLE; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2136 | rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, 0, false, |
| 2137 | &new_desc); |
| 2138 | if (rc != X86EMUL_CONTINUE) |
| 2139 | return rc; |
| 2140 | rc = assign_eip_far(ctxt, eip, new_desc.l); |
| 2141 | if (rc != X86EMUL_CONTINUE) { |
Nadav Amit | cd9b8e2c | 2014-10-28 00:03:43 +0200 | [diff] [blame] | 2142 | WARN_ON(ctxt->mode != X86EMUL_MODE_PROT64); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2143 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 2144 | } |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2145 | return rc; |
| 2146 | } |
| 2147 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2148 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2149 | { |
| 2150 | int rc; |
| 2151 | |
| 2152 | rc = em_ret_far(ctxt); |
| 2153 | if (rc != X86EMUL_CONTINUE) |
| 2154 | return rc; |
| 2155 | rsp_increment(ctxt, ctxt->src.val); |
| 2156 | return X86EMUL_CONTINUE; |
| 2157 | } |
| 2158 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2159 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2160 | { |
| 2161 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2162 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2163 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2164 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2165 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2166 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2167 | |
| 2168 | if (ctxt->eflags & EFLG_ZF) { |
| 2169 | /* Success: write back to memory. */ |
| 2170 | ctxt->dst.val = ctxt->src.orig_val; |
| 2171 | } else { |
| 2172 | /* Failure: write the value we saw to EAX. */ |
| 2173 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2174 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2175 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2176 | } |
| 2177 | return X86EMUL_CONTINUE; |
| 2178 | } |
| 2179 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2180 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2181 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2182 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2183 | unsigned short sel; |
| 2184 | int rc; |
| 2185 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2186 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2187 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2188 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2189 | if (rc != X86EMUL_CONTINUE) |
| 2190 | return rc; |
| 2191 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2192 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2193 | return rc; |
| 2194 | } |
| 2195 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2196 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2197 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2198 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2199 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2200 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2201 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2202 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2203 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2204 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2205 | cs->s = 1; |
| 2206 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2207 | cs->p = 1; |
| 2208 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2209 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2210 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2211 | set_desc_base(ss, 0); /* flat segment */ |
| 2212 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2213 | ss->g = 1; /* 4kb granularity */ |
| 2214 | ss->s = 1; |
| 2215 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2216 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2217 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2218 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2219 | ss->l = 0; |
| 2220 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2223 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2224 | { |
| 2225 | u32 eax, ebx, ecx, edx; |
| 2226 | |
| 2227 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2228 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2229 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2230 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2231 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2232 | } |
| 2233 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2234 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2235 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2236 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2237 | u32 eax, ebx, ecx, edx; |
| 2238 | |
| 2239 | /* |
| 2240 | * syscall should always be enabled in longmode - so only become |
| 2241 | * vendor specific (cpuid) if other modes are active... |
| 2242 | */ |
| 2243 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2244 | return true; |
| 2245 | |
| 2246 | eax = 0x00000000; |
| 2247 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2248 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2249 | /* |
| 2250 | * Intel ("GenuineIntel") |
| 2251 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2252 | * longmode. Also an 64bit guest with a |
| 2253 | * 32bit compat-app running will #UD !! While this |
| 2254 | * behaviour can be fixed (by emulating) into AMD |
| 2255 | * response - CPUs of AMD can't behave like Intel. |
| 2256 | */ |
| 2257 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2258 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2259 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2260 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2261 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2262 | /* AMD ("AuthenticAMD") */ |
| 2263 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2264 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2265 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2266 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2267 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2268 | /* AMD ("AMDisbetter!") */ |
| 2269 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2270 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2271 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2272 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2273 | |
| 2274 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2278 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2279 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2280 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2281 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2282 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2283 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2284 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2285 | |
| 2286 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2287 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2288 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2289 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2290 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2291 | if (!(em_syscall_is_enabled(ctxt))) |
| 2292 | return emulate_ud(ctxt); |
| 2293 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2294 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2295 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2296 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2297 | if (!(efer & EFER_SCE)) |
| 2298 | return emulate_ud(ctxt); |
| 2299 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2300 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2301 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2302 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2303 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2304 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2305 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2306 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2307 | cs.l = 1; |
| 2308 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2309 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2310 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2311 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2312 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2313 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2314 | #ifdef CONFIG_X86_64 |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2315 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2316 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2317 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2318 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2319 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2320 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2321 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2322 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2323 | ctxt->eflags &= ~msr_data; |
Nadav Amit | 807c142 | 2014-11-02 11:54:49 +0200 | [diff] [blame] | 2324 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2325 | #endif |
| 2326 | } else { |
| 2327 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2328 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2329 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2330 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2331 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2332 | } |
| 2333 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2334 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2337 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2338 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2339 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2340 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2341 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2342 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2343 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2344 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2345 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2346 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2347 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2348 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2349 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2350 | /* |
| 2351 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2352 | * mode). |
| 2353 | */ |
| 2354 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2355 | && !vendor_intel(ctxt)) |
| 2356 | return emulate_ud(ctxt); |
| 2357 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2358 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2359 | * Therefore, we inject an #UD. |
| 2360 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2361 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2362 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2363 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2364 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2365 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2366 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2367 | switch (ctxt->mode) { |
| 2368 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2369 | if ((msr_data & 0xfffc) == 0x0) |
| 2370 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2371 | break; |
| 2372 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2373 | if (msr_data == 0x0) |
| 2374 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2375 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2376 | default: |
| 2377 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
Nadav Amit | 6c6cb69 | 2014-07-21 14:37:30 +0300 | [diff] [blame] | 2380 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2381 | cs_sel = (u16)msr_data; |
| 2382 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2383 | ss_sel = cs_sel + 8; |
| 2384 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2385 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2386 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2387 | cs.l = 1; |
| 2388 | } |
| 2389 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2390 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2391 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2392 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2393 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2394 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2395 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2396 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2397 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2398 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2399 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2402 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2403 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2404 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2405 | struct desc_struct cs, ss; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2406 | u64 msr_data, rcx, rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2407 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2408 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2409 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2410 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2411 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2412 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2413 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2414 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2415 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2416 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2417 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2418 | usermode = X86EMUL_MODE_PROT64; |
| 2419 | else |
| 2420 | usermode = X86EMUL_MODE_PROT32; |
| 2421 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2422 | rcx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2423 | rdx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2424 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2425 | cs.dpl = 3; |
| 2426 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2427 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2428 | switch (usermode) { |
| 2429 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2430 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2431 | if ((msr_data & 0xfffc) == 0x0) |
| 2432 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2433 | ss_sel = (u16)(msr_data + 24); |
Nadav Amit | bf0b682 | 2014-09-18 22:39:45 +0300 | [diff] [blame] | 2434 | rcx = (u32)rcx; |
| 2435 | rdx = (u32)rdx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2436 | break; |
| 2437 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2438 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2439 | if (msr_data == 0x0) |
| 2440 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2441 | ss_sel = cs_sel + 8; |
| 2442 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2443 | cs.l = 1; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2444 | if (is_noncanonical_address(rcx) || |
| 2445 | is_noncanonical_address(rdx)) |
| 2446 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2447 | break; |
| 2448 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2449 | cs_sel |= SELECTOR_RPL_MASK; |
| 2450 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2451 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2452 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2453 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2454 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 2455 | ctxt->_eip = rdx; |
| 2456 | *reg_write(ctxt, VCPU_REGS_RSP) = rcx; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2457 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2458 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2459 | } |
| 2460 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2461 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2462 | { |
| 2463 | int iopl; |
| 2464 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2465 | return false; |
| 2466 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2467 | return true; |
| 2468 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2469 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2473 | u16 port, u16 len) |
| 2474 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2475 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2476 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2477 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2478 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2479 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2480 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2481 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2482 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2483 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2484 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2485 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2486 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2487 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2488 | base = get_desc_base(&tr_seg); |
| 2489 | #ifdef CONFIG_X86_64 |
| 2490 | base |= ((u64)base3) << 32; |
| 2491 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2492 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2493 | if (r != X86EMUL_CONTINUE) |
| 2494 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2495 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2496 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2497 | 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] | 2498 | if (r != X86EMUL_CONTINUE) |
| 2499 | return false; |
| 2500 | if ((perm >> bit_idx) & mask) |
| 2501 | return false; |
| 2502 | return true; |
| 2503 | } |
| 2504 | |
| 2505 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2506 | u16 port, u16 len) |
| 2507 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2508 | if (ctxt->perm_ok) |
| 2509 | return true; |
| 2510 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2511 | if (emulator_bad_iopl(ctxt)) |
| 2512 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2513 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2514 | |
| 2515 | ctxt->perm_ok = true; |
| 2516 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2517 | return true; |
| 2518 | } |
| 2519 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2520 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2521 | struct tss_segment_16 *tss) |
| 2522 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2523 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2524 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2525 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2526 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2527 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2528 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2529 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2530 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2531 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2532 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2533 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2534 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2535 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2536 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2537 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2538 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2542 | struct tss_segment_16 *tss) |
| 2543 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2544 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2545 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2546 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2547 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2548 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2549 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2550 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2551 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2552 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2553 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2554 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2555 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2556 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2557 | |
| 2558 | /* |
| 2559 | * SDM says that segment selectors are loaded before segment |
| 2560 | * descriptors |
| 2561 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2562 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2563 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2564 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2565 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2566 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2567 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2568 | cpl = tss->cs & 3; |
| 2569 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2570 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2571 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2572 | * it is handled in a context of new task |
| 2573 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2574 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, |
| 2575 | true, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2576 | if (ret != X86EMUL_CONTINUE) |
| 2577 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2578 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
| 2579 | true, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2580 | if (ret != X86EMUL_CONTINUE) |
| 2581 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2582 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, |
| 2583 | true, 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->ss, VCPU_SREG_SS, cpl, |
| 2587 | true, 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->ds, VCPU_SREG_DS, cpl, |
| 2591 | true, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2592 | if (ret != X86EMUL_CONTINUE) |
| 2593 | return ret; |
| 2594 | |
| 2595 | return X86EMUL_CONTINUE; |
| 2596 | } |
| 2597 | |
| 2598 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2599 | u16 tss_selector, u16 old_tss_sel, |
| 2600 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2601 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2602 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2603 | struct tss_segment_16 tss_seg; |
| 2604 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2605 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2606 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2607 | 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] | 2608 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2609 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2610 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2611 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2612 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2613 | save_state_to_tss16(ctxt, &tss_seg); |
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->write_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 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2619 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2620 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2621 | 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] | 2622 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2623 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2624 | /* FIXME: need to provide precise fault address */ |
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 | |
| 2627 | if (old_tss_sel != 0xffff) { |
| 2628 | tss_seg.prev_task_link = old_tss_sel; |
| 2629 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2630 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | &tss_seg.prev_task_link, |
| 2632 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2633 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2634 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2635 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2636 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2637 | } |
| 2638 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2639 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2643 | struct tss_segment_32 *tss) |
| 2644 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2645 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2646 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2648 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2649 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2650 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2651 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2652 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2653 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2654 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2655 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2656 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2657 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2658 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2659 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2660 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2661 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2662 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | struct tss_segment_32 *tss) |
| 2667 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2668 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2669 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2670 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2671 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2672 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2673 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2674 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2675 | |
| 2676 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2677 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2678 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2679 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2680 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2681 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2682 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2683 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2684 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* |
| 2687 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2688 | * descriptors. This is important because CPL checks will |
| 2689 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2690 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2691 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2692 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2693 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2694 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2695 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2696 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2697 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2698 | |
| 2699 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2700 | * If we're switching between Protected Mode and VM86, we need to make |
| 2701 | * sure to update the mode before loading the segment descriptors so |
| 2702 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2703 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2704 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2705 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2706 | cpl = 3; |
| 2707 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2708 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2709 | cpl = tss->cs & 3; |
| 2710 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2711 | |
| 2712 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2713 | * Now load segment descriptors. If fault happenes at this stage |
| 2714 | * it is handled in a context of new task |
| 2715 | */ |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2716 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, |
| 2717 | cpl, true, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2718 | if (ret != X86EMUL_CONTINUE) |
| 2719 | return ret; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 2720 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, |
| 2721 | true, 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->cs, VCPU_SREG_CS, cpl, |
| 2725 | true, 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->ss, VCPU_SREG_SS, cpl, |
| 2729 | true, 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->ds, VCPU_SREG_DS, cpl, |
| 2733 | true, 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->fs, VCPU_SREG_FS, cpl, |
| 2737 | true, 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->gs, VCPU_SREG_GS, cpl, |
| 2741 | true, NULL); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2742 | if (ret != X86EMUL_CONTINUE) |
| 2743 | return ret; |
| 2744 | |
| 2745 | return X86EMUL_CONTINUE; |
| 2746 | } |
| 2747 | |
| 2748 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2749 | u16 tss_selector, u16 old_tss_sel, |
| 2750 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2751 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2752 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2753 | struct tss_segment_32 tss_seg; |
| 2754 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2755 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2756 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2757 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2758 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2759 | 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] | 2760 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2761 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2762 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2763 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2764 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2765 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2766 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2767 | /* Only GP registers and segment selectors are saved */ |
| 2768 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2769 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2770 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2771 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2772 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2773 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2774 | 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] | 2775 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2776 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2777 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2778 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2779 | |
| 2780 | if (old_tss_sel != 0xffff) { |
| 2781 | tss_seg.prev_task_link = old_tss_sel; |
| 2782 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2783 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2784 | &tss_seg.prev_task_link, |
| 2785 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2786 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2787 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2788 | /* FIXME: need to provide precise fault address */ |
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 |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2824 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2825 | */ |
| 2826 | if (reason == TASK_SWITCH_GATE) { |
| 2827 | if (idt_index != -1) { |
| 2828 | /* Software interrupts */ |
| 2829 | struct desc_struct task_gate_desc; |
| 2830 | int dpl; |
| 2831 | |
| 2832 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2833 | &task_gate_desc); |
| 2834 | if (ret != X86EMUL_CONTINUE) |
| 2835 | return ret; |
| 2836 | |
| 2837 | dpl = task_gate_desc.dpl; |
| 2838 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2839 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2840 | } |
| 2841 | } else if (reason != TASK_SWITCH_IRET) { |
| 2842 | int dpl = next_tss_desc.dpl; |
| 2843 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2844 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2847 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2848 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2849 | if (!next_tss_desc.p || |
| 2850 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2851 | desc_limit < 0x2b)) { |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 2852 | return emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2856 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2857 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | if (reason == TASK_SWITCH_IRET) |
| 2861 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2862 | |
| 2863 | /* 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] | 2864 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2865 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2866 | old_tss_sel = 0xffff; |
| 2867 | |
| 2868 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2869 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2870 | old_tss_base, &next_tss_desc); |
| 2871 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2872 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2873 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2874 | if (ret != X86EMUL_CONTINUE) |
| 2875 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2876 | |
| 2877 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2878 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2879 | |
| 2880 | if (reason != TASK_SWITCH_IRET) { |
| 2881 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2882 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2883 | } |
| 2884 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2885 | 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] | 2886 | 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] | 2887 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2888 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2889 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2890 | ctxt->lock_prefix = 0; |
| 2891 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2892 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2895 | return ret; |
| 2896 | } |
| 2897 | |
| 2898 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2899 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2900 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2901 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2902 | int rc; |
| 2903 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2904 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2905 | ctxt->_eip = ctxt->eip; |
| 2906 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2907 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2908 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2909 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2910 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2911 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2912 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2913 | writeback_registers(ctxt); |
| 2914 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2915 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2916 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2917 | } |
| 2918 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2919 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2920 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2921 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2922 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2923 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2924 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2925 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2926 | } |
| 2927 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2928 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2929 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2930 | u8 al, old_al; |
| 2931 | bool af, cf, old_cf; |
| 2932 | |
| 2933 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2934 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2935 | |
| 2936 | old_al = al; |
| 2937 | old_cf = cf; |
| 2938 | cf = false; |
| 2939 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2940 | if ((al & 0x0f) > 9 || af) { |
| 2941 | al -= 6; |
| 2942 | cf = old_cf | (al >= 250); |
| 2943 | af = true; |
| 2944 | } else { |
| 2945 | af = false; |
| 2946 | } |
| 2947 | if (old_al > 0x99 || old_cf) { |
| 2948 | al -= 0x60; |
| 2949 | cf = true; |
| 2950 | } |
| 2951 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2952 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2953 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2954 | ctxt->src.type = OP_IMM; |
| 2955 | ctxt->src.val = 0; |
| 2956 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2957 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2958 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2959 | if (cf) |
| 2960 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2961 | if (af) |
| 2962 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2963 | return X86EMUL_CONTINUE; |
| 2964 | } |
| 2965 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2966 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2967 | { |
| 2968 | u8 al, ah; |
| 2969 | |
| 2970 | if (ctxt->src.val == 0) |
| 2971 | return emulate_de(ctxt); |
| 2972 | |
| 2973 | al = ctxt->dst.val & 0xff; |
| 2974 | ah = al / ctxt->src.val; |
| 2975 | al %= ctxt->src.val; |
| 2976 | |
| 2977 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 2978 | |
| 2979 | /* Set PF, ZF, SF */ |
| 2980 | ctxt->src.type = OP_IMM; |
| 2981 | ctxt->src.val = 0; |
| 2982 | ctxt->src.bytes = 1; |
| 2983 | fastop(ctxt, em_or); |
| 2984 | |
| 2985 | return X86EMUL_CONTINUE; |
| 2986 | } |
| 2987 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2988 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2989 | { |
| 2990 | u8 al = ctxt->dst.val & 0xff; |
| 2991 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2992 | |
| 2993 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2994 | |
| 2995 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2996 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 2997 | /* Set PF, ZF, SF */ |
| 2998 | ctxt->src.type = OP_IMM; |
| 2999 | ctxt->src.val = 0; |
| 3000 | ctxt->src.bytes = 1; |
| 3001 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3002 | |
| 3003 | return X86EMUL_CONTINUE; |
| 3004 | } |
| 3005 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3006 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3007 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3008 | int rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3009 | long rel = ctxt->src.val; |
| 3010 | |
| 3011 | ctxt->src.val = (unsigned long)ctxt->_eip; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3012 | rc = jmp_rel(ctxt, rel); |
| 3013 | if (rc != X86EMUL_CONTINUE) |
| 3014 | return rc; |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3015 | return em_push(ctxt); |
| 3016 | } |
| 3017 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3018 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3019 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3020 | u16 sel, old_cs; |
| 3021 | ulong old_eip; |
| 3022 | int rc; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3023 | struct desc_struct old_desc, new_desc; |
| 3024 | const struct x86_emulate_ops *ops = ctxt->ops; |
| 3025 | int cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3026 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3027 | old_eip = ctxt->_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3028 | ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3029 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3030 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3031 | rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl, false, |
| 3032 | &new_desc); |
| 3033 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3034 | return X86EMUL_CONTINUE; |
| 3035 | |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3036 | rc = assign_eip_far(ctxt, ctxt->src.val, new_desc.l); |
| 3037 | if (rc != X86EMUL_CONTINUE) |
| 3038 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3039 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3040 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3041 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3042 | if (rc != X86EMUL_CONTINUE) |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3043 | goto fail; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3044 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3045 | ctxt->src.val = old_eip; |
Nadav Amit | d1442d8 | 2014-09-18 22:39:39 +0300 | [diff] [blame] | 3046 | rc = em_push(ctxt); |
| 3047 | /* If we failed, we tainted the memory, but the very least we should |
| 3048 | restore cs */ |
| 3049 | if (rc != X86EMUL_CONTINUE) |
| 3050 | goto fail; |
| 3051 | return rc; |
| 3052 | fail: |
| 3053 | ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS); |
| 3054 | return rc; |
| 3055 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3056 | } |
| 3057 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3058 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3059 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3060 | int rc; |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3061 | unsigned long eip; |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3062 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3063 | rc = emulate_pop(ctxt, &eip, ctxt->op_bytes); |
| 3064 | if (rc != X86EMUL_CONTINUE) |
| 3065 | return rc; |
| 3066 | rc = assign_eip_near(ctxt, eip); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3067 | if (rc != X86EMUL_CONTINUE) |
| 3068 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3069 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3070 | return X86EMUL_CONTINUE; |
| 3071 | } |
| 3072 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3073 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3074 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3075 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3076 | ctxt->src.val = ctxt->dst.val; |
| 3077 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3078 | |
| 3079 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3080 | ctxt->dst.val = ctxt->src.orig_val; |
| 3081 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3082 | return X86EMUL_CONTINUE; |
| 3083 | } |
| 3084 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3085 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3086 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3087 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3088 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3089 | } |
| 3090 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3091 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3092 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3093 | ctxt->dst.type = OP_REG; |
| 3094 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3095 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3096 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3097 | |
| 3098 | return X86EMUL_CONTINUE; |
| 3099 | } |
| 3100 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3101 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3102 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3103 | u64 tsc = 0; |
| 3104 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3105 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3106 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3107 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3108 | return X86EMUL_CONTINUE; |
| 3109 | } |
| 3110 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3111 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3112 | { |
| 3113 | u64 pmc; |
| 3114 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3115 | 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] | 3116 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3117 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3118 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3119 | return X86EMUL_CONTINUE; |
| 3120 | } |
| 3121 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3122 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3123 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 3124 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3125 | return X86EMUL_CONTINUE; |
| 3126 | } |
| 3127 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3128 | #define FFL(x) bit(X86_FEATURE_##x) |
| 3129 | |
| 3130 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 3131 | { |
| 3132 | u32 ebx, ecx, edx, eax = 1; |
| 3133 | u16 tmp; |
| 3134 | |
| 3135 | /* |
| 3136 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 3137 | */ |
| 3138 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 3139 | if (!(ecx & FFL(MOVBE))) |
| 3140 | return emulate_ud(ctxt); |
| 3141 | |
| 3142 | switch (ctxt->op_bytes) { |
| 3143 | case 2: |
| 3144 | /* |
| 3145 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3146 | * the upper word of the destination register remains unchanged |
| 3147 | * ..." |
| 3148 | * |
| 3149 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3150 | * rules so we have to do the operation almost per hand. |
| 3151 | */ |
| 3152 | tmp = (u16)ctxt->src.val; |
| 3153 | ctxt->dst.val &= ~0xffffUL; |
| 3154 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3155 | break; |
| 3156 | case 4: |
| 3157 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3158 | break; |
| 3159 | case 8: |
| 3160 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3161 | break; |
| 3162 | default: |
Paolo Bonzini | 592f085 | 2014-08-20 10:05:08 +0200 | [diff] [blame] | 3163 | BUG(); |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3164 | } |
| 3165 | return X86EMUL_CONTINUE; |
| 3166 | } |
| 3167 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3168 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3169 | { |
| 3170 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3171 | return emulate_gp(ctxt, 0); |
| 3172 | |
| 3173 | /* Disable writeback. */ |
| 3174 | ctxt->dst.type = OP_NONE; |
| 3175 | return X86EMUL_CONTINUE; |
| 3176 | } |
| 3177 | |
| 3178 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3179 | { |
| 3180 | unsigned long val; |
| 3181 | |
| 3182 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3183 | val = ctxt->src.val & ~0ULL; |
| 3184 | else |
| 3185 | val = ctxt->src.val & ~0U; |
| 3186 | |
| 3187 | /* #UD condition is already handled. */ |
| 3188 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3189 | return emulate_gp(ctxt, 0); |
| 3190 | |
| 3191 | /* Disable writeback. */ |
| 3192 | ctxt->dst.type = OP_NONE; |
| 3193 | return X86EMUL_CONTINUE; |
| 3194 | } |
| 3195 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3196 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3197 | { |
| 3198 | u64 msr_data; |
| 3199 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3200 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3201 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3202 | 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] | 3203 | return emulate_gp(ctxt, 0); |
| 3204 | |
| 3205 | return X86EMUL_CONTINUE; |
| 3206 | } |
| 3207 | |
| 3208 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3209 | { |
| 3210 | u64 msr_data; |
| 3211 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3212 | 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] | 3213 | return emulate_gp(ctxt, 0); |
| 3214 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3215 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3216 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3217 | return X86EMUL_CONTINUE; |
| 3218 | } |
| 3219 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3220 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3221 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3222 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3223 | return emulate_ud(ctxt); |
| 3224 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3225 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Nadav Amit | b5bbf10 | 2014-11-02 11:54:46 +0200 | [diff] [blame] | 3226 | if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM) |
| 3227 | ctxt->dst.bytes = 2; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3228 | return X86EMUL_CONTINUE; |
| 3229 | } |
| 3230 | |
| 3231 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3232 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3233 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3234 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3235 | 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] | 3236 | return emulate_ud(ctxt); |
| 3237 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3238 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3239 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3240 | |
| 3241 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3242 | ctxt->dst.type = OP_NONE; |
| 3243 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3244 | } |
| 3245 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3246 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3247 | { |
| 3248 | u16 sel = ctxt->src.val; |
| 3249 | |
| 3250 | /* Disable writeback. */ |
| 3251 | ctxt->dst.type = OP_NONE; |
| 3252 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3253 | } |
| 3254 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3255 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3256 | { |
| 3257 | u16 sel = ctxt->src.val; |
| 3258 | |
| 3259 | /* Disable writeback. */ |
| 3260 | ctxt->dst.type = OP_NONE; |
| 3261 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3262 | } |
| 3263 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3264 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3265 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3266 | int rc; |
| 3267 | ulong linear; |
| 3268 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3269 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3270 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3271 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3272 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3273 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3274 | return X86EMUL_CONTINUE; |
| 3275 | } |
| 3276 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3277 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3278 | { |
| 3279 | ulong cr0; |
| 3280 | |
| 3281 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3282 | cr0 &= ~X86_CR0_TS; |
| 3283 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3284 | return X86EMUL_CONTINUE; |
| 3285 | } |
| 3286 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3287 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3288 | { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3289 | int rc = ctxt->ops->fix_hypercall(ctxt); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3290 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3291 | if (rc != X86EMUL_CONTINUE) |
| 3292 | return rc; |
| 3293 | |
| 3294 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3295 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3296 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3297 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3298 | return X86EMUL_CONTINUE; |
| 3299 | } |
| 3300 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3301 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3302 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3303 | struct desc_ptr *ptr)) |
| 3304 | { |
| 3305 | struct desc_ptr desc_ptr; |
| 3306 | |
| 3307 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3308 | ctxt->op_bytes = 8; |
| 3309 | get(ctxt, &desc_ptr); |
| 3310 | if (ctxt->op_bytes == 2) { |
| 3311 | ctxt->op_bytes = 4; |
| 3312 | desc_ptr.address &= 0x00ffffff; |
| 3313 | } |
| 3314 | /* Disable writeback. */ |
| 3315 | ctxt->dst.type = OP_NONE; |
| 3316 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3317 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3318 | } |
| 3319 | |
| 3320 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3321 | { |
| 3322 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3323 | } |
| 3324 | |
| 3325 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3326 | { |
| 3327 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3328 | } |
| 3329 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3330 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3331 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3332 | struct desc_ptr desc_ptr; |
| 3333 | int rc; |
| 3334 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3335 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3336 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3337 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3338 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3339 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3340 | if (rc != X86EMUL_CONTINUE) |
| 3341 | return rc; |
| 3342 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3343 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3344 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3345 | return X86EMUL_CONTINUE; |
| 3346 | } |
| 3347 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3348 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3349 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3350 | int rc; |
| 3351 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3352 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3353 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3354 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3355 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3356 | return rc; |
| 3357 | } |
| 3358 | |
| 3359 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3360 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3361 | struct desc_ptr desc_ptr; |
| 3362 | int rc; |
| 3363 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3364 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3365 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3366 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3367 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3368 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3369 | if (rc != X86EMUL_CONTINUE) |
| 3370 | return rc; |
| 3371 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3372 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3373 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3374 | return X86EMUL_CONTINUE; |
| 3375 | } |
| 3376 | |
| 3377 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3378 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3379 | if (ctxt->dst.type == OP_MEM) |
| 3380 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3381 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3382 | return X86EMUL_CONTINUE; |
| 3383 | } |
| 3384 | |
| 3385 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3386 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3387 | 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] | 3388 | | (ctxt->src.val & 0x0f)); |
| 3389 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3390 | return X86EMUL_CONTINUE; |
| 3391 | } |
| 3392 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3393 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3394 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3395 | int rc = X86EMUL_CONTINUE; |
| 3396 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3397 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3398 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3399 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3400 | rc = jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3401 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3402 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3406 | { |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3407 | int rc = X86EMUL_CONTINUE; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3408 | |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 3409 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
| 3410 | rc = jmp_rel(ctxt, ctxt->src.val); |
| 3411 | |
| 3412 | return rc; |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3413 | } |
| 3414 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3415 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3416 | { |
| 3417 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3418 | &ctxt->dst.val)) |
| 3419 | return X86EMUL_IO_NEEDED; |
| 3420 | |
| 3421 | return X86EMUL_CONTINUE; |
| 3422 | } |
| 3423 | |
| 3424 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3425 | { |
| 3426 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3427 | &ctxt->src.val, 1); |
| 3428 | /* Disable writeback. */ |
| 3429 | ctxt->dst.type = OP_NONE; |
| 3430 | return X86EMUL_CONTINUE; |
| 3431 | } |
| 3432 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3433 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3434 | { |
| 3435 | if (emulator_bad_iopl(ctxt)) |
| 3436 | return emulate_gp(ctxt, 0); |
| 3437 | |
| 3438 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3439 | return X86EMUL_CONTINUE; |
| 3440 | } |
| 3441 | |
| 3442 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3443 | { |
| 3444 | if (emulator_bad_iopl(ctxt)) |
| 3445 | return emulate_gp(ctxt, 0); |
| 3446 | |
| 3447 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3448 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3449 | return X86EMUL_CONTINUE; |
| 3450 | } |
| 3451 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3452 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3453 | { |
| 3454 | u32 eax, ebx, ecx, edx; |
| 3455 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3456 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3457 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3458 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3459 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3460 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3461 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3462 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3463 | return X86EMUL_CONTINUE; |
| 3464 | } |
| 3465 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3466 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3467 | { |
| 3468 | u32 flags; |
| 3469 | |
| 3470 | flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF; |
| 3471 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3472 | |
| 3473 | ctxt->eflags &= ~0xffUL; |
| 3474 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3475 | return X86EMUL_CONTINUE; |
| 3476 | } |
| 3477 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3478 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3479 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3480 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3481 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3482 | return X86EMUL_CONTINUE; |
| 3483 | } |
| 3484 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3485 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3486 | { |
| 3487 | switch (ctxt->op_bytes) { |
| 3488 | #ifdef CONFIG_X86_64 |
| 3489 | case 8: |
| 3490 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3491 | break; |
| 3492 | #endif |
| 3493 | default: |
| 3494 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3495 | break; |
| 3496 | } |
| 3497 | return X86EMUL_CONTINUE; |
| 3498 | } |
| 3499 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3500 | static int em_clflush(struct x86_emulate_ctxt *ctxt) |
| 3501 | { |
| 3502 | /* emulating clflush regardless of cpuid */ |
| 3503 | return X86EMUL_CONTINUE; |
| 3504 | } |
| 3505 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3506 | static bool valid_cr(int nr) |
| 3507 | { |
| 3508 | switch (nr) { |
| 3509 | case 0: |
| 3510 | case 2 ... 4: |
| 3511 | case 8: |
| 3512 | return true; |
| 3513 | default: |
| 3514 | return false; |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3519 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3520 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3521 | return emulate_ud(ctxt); |
| 3522 | |
| 3523 | return X86EMUL_CONTINUE; |
| 3524 | } |
| 3525 | |
| 3526 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3527 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3528 | u64 new_val = ctxt->src.val64; |
| 3529 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3530 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3531 | |
| 3532 | static u64 cr_reserved_bits[] = { |
| 3533 | 0xffffffff00000000ULL, |
| 3534 | 0, 0, 0, /* CR3 checked later */ |
| 3535 | CR4_RESERVED_BITS, |
| 3536 | 0, 0, 0, |
| 3537 | CR8_RESERVED_BITS, |
| 3538 | }; |
| 3539 | |
| 3540 | if (!valid_cr(cr)) |
| 3541 | return emulate_ud(ctxt); |
| 3542 | |
| 3543 | if (new_val & cr_reserved_bits[cr]) |
| 3544 | return emulate_gp(ctxt, 0); |
| 3545 | |
| 3546 | switch (cr) { |
| 3547 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3548 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3549 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3550 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3551 | return emulate_gp(ctxt, 0); |
| 3552 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3553 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3554 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3555 | |
| 3556 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3557 | !(cr4 & X86_CR4_PAE)) |
| 3558 | return emulate_gp(ctxt, 0); |
| 3559 | |
| 3560 | break; |
| 3561 | } |
| 3562 | case 3: { |
| 3563 | u64 rsvd = 0; |
| 3564 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3565 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3566 | if (efer & EFER_LMA) |
Nadav Amit | 9d88fca | 2014-11-02 11:54:52 +0200 | [diff] [blame] | 3567 | rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3568 | |
| 3569 | if (new_val & rsvd) |
| 3570 | return emulate_gp(ctxt, 0); |
| 3571 | |
| 3572 | break; |
| 3573 | } |
| 3574 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3575 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3576 | |
| 3577 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3578 | return emulate_gp(ctxt, 0); |
| 3579 | |
| 3580 | break; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | return X86EMUL_CONTINUE; |
| 3585 | } |
| 3586 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3587 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3588 | { |
| 3589 | unsigned long dr7; |
| 3590 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3591 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3592 | |
| 3593 | /* Check if DR7.Global_Enable is set */ |
| 3594 | return dr7 & (1 << 13); |
| 3595 | } |
| 3596 | |
| 3597 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3598 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3599 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3600 | u64 cr4; |
| 3601 | |
| 3602 | if (dr > 7) |
| 3603 | return emulate_ud(ctxt); |
| 3604 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3605 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3606 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3607 | return emulate_ud(ctxt); |
| 3608 | |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3609 | if (check_dr7_gd(ctxt)) { |
| 3610 | ulong dr6; |
| 3611 | |
| 3612 | ctxt->ops->get_dr(ctxt, 6, &dr6); |
| 3613 | dr6 &= ~15; |
| 3614 | dr6 |= DR6_BD | DR6_RTM; |
| 3615 | ctxt->ops->set_dr(ctxt, 6, dr6); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3616 | return emulate_db(ctxt); |
Nadav Amit | 6d2a052 | 2014-11-02 11:54:43 +0200 | [diff] [blame] | 3617 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3618 | |
| 3619 | return X86EMUL_CONTINUE; |
| 3620 | } |
| 3621 | |
| 3622 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3623 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3624 | u64 new_val = ctxt->src.val64; |
| 3625 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3626 | |
| 3627 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3628 | return emulate_gp(ctxt, 0); |
| 3629 | |
| 3630 | return check_dr_read(ctxt); |
| 3631 | } |
| 3632 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3633 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3634 | { |
| 3635 | u64 efer; |
| 3636 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3637 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3638 | |
| 3639 | if (!(efer & EFER_SVME)) |
| 3640 | return emulate_ud(ctxt); |
| 3641 | |
| 3642 | return X86EMUL_CONTINUE; |
| 3643 | } |
| 3644 | |
| 3645 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3646 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3647 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3648 | |
| 3649 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3650 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3651 | return emulate_gp(ctxt, 0); |
| 3652 | |
| 3653 | return check_svme(ctxt); |
| 3654 | } |
| 3655 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3656 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3657 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3658 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3659 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3660 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3661 | return emulate_ud(ctxt); |
| 3662 | |
| 3663 | return X86EMUL_CONTINUE; |
| 3664 | } |
| 3665 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3666 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3667 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3668 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3669 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3670 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3671 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3672 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3673 | return emulate_gp(ctxt, 0); |
| 3674 | |
| 3675 | return X86EMUL_CONTINUE; |
| 3676 | } |
| 3677 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3678 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3679 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3680 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3681 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3682 | return emulate_gp(ctxt, 0); |
| 3683 | |
| 3684 | return X86EMUL_CONTINUE; |
| 3685 | } |
| 3686 | |
| 3687 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3688 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3689 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3690 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3691 | return emulate_gp(ctxt, 0); |
| 3692 | |
| 3693 | return X86EMUL_CONTINUE; |
| 3694 | } |
| 3695 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3696 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3697 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 3698 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 3699 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3700 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3701 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3702 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3703 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3704 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3705 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3706 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3707 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3708 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3709 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3710 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 3711 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3712 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3713 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3714 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3715 | #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] | 3716 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3717 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3718 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3719 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3720 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3721 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3722 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3723 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3724 | |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3725 | static const struct opcode group7_rm0[] = { |
| 3726 | N, |
| 3727 | I(SrcNone | Priv | EmulateOnUD, em_vmcall), |
| 3728 | N, N, N, N, N, N, |
| 3729 | }; |
| 3730 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3731 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3732 | DI(SrcNone | Priv, monitor), |
| 3733 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3734 | N, N, N, N, N, N, |
| 3735 | }; |
| 3736 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3737 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3738 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3739 | II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3740 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3741 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3742 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3743 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3744 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3745 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3746 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3747 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3748 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3749 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3750 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3751 | N, N, N, N, N, N, |
| 3752 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3753 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3754 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3755 | F(Lock, em_add), |
| 3756 | F(Lock | PageTable, em_or), |
| 3757 | F(Lock, em_adc), |
| 3758 | F(Lock, em_sbb), |
| 3759 | F(Lock | PageTable, em_and), |
| 3760 | F(Lock, em_sub), |
| 3761 | F(Lock, em_xor), |
| 3762 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3763 | }; |
| 3764 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3765 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3766 | 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] | 3767 | }; |
| 3768 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3769 | static const struct opcode group2[] = { |
| 3770 | F(DstMem | ModRM, em_rol), |
| 3771 | F(DstMem | ModRM, em_ror), |
| 3772 | F(DstMem | ModRM, em_rcl), |
| 3773 | F(DstMem | ModRM, em_rcr), |
| 3774 | F(DstMem | ModRM, em_shl), |
| 3775 | F(DstMem | ModRM, em_shr), |
| 3776 | F(DstMem | ModRM, em_shl), |
| 3777 | F(DstMem | ModRM, em_sar), |
| 3778 | }; |
| 3779 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3780 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3781 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3782 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3783 | F(DstMem | SrcNone | Lock, em_not), |
| 3784 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3785 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3786 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3787 | F(DstXacc | Src2Mem, em_div_ex), |
| 3788 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3789 | }; |
| 3790 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3791 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3792 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3793 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3794 | N, N, N, N, N, N, |
| 3795 | }; |
| 3796 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3797 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3798 | F(DstMem | SrcNone | Lock, em_inc), |
| 3799 | F(DstMem | SrcNone | Lock, em_dec), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3800 | I(SrcMem | NearBranch, em_call_near_abs), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3801 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3802 | I(SrcMem | NearBranch, em_jmp_abs), |
Nadav Amit | f778404 | 2014-09-18 22:39:41 +0300 | [diff] [blame] | 3803 | I(SrcMemFAddr | ImplicitOps, em_jmp_far), |
| 3804 | I(SrcMem | Stack, em_push), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3805 | }; |
| 3806 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3807 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3808 | DI(Prot, sldt), |
| 3809 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3810 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3811 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3812 | N, N, N, N, |
| 3813 | }; |
| 3814 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3815 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3816 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3817 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3818 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3819 | II(SrcMem | Priv, em_lidt, lidt), |
| 3820 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3821 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3822 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3823 | }, { |
Nadav Amit | 0f54a32 | 2014-08-29 11:26:55 +0300 | [diff] [blame] | 3824 | EXT(0, group7_rm0), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3825 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3826 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3827 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3828 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3829 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3830 | } }; |
| 3831 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3832 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3833 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3834 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3835 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3836 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3837 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3838 | }; |
| 3839 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3840 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3841 | 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] | 3842 | }, { |
| 3843 | N, N, N, N, N, N, N, N, |
| 3844 | } }; |
| 3845 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3846 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3847 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3848 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3849 | }; |
| 3850 | |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3851 | static const struct gprefix pfx_0f_ae_7 = { |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 3852 | I(SrcMem | ByteOp, em_clflush), N, N, N, |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 3853 | }; |
| 3854 | |
| 3855 | static const struct group_dual group15 = { { |
| 3856 | N, N, N, N, N, N, N, GP(0, &pfx_0f_ae_7), |
| 3857 | }, { |
| 3858 | N, N, N, N, N, N, N, N, |
| 3859 | } }; |
| 3860 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3861 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3862 | 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] | 3863 | }; |
| 3864 | |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 3865 | static const struct gprefix pfx_0f_2b = { |
| 3866 | I(0, em_mov), I(0, em_mov), N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3867 | }; |
| 3868 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3869 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3870 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3871 | }; |
| 3872 | |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 3873 | static const struct gprefix pfx_0f_e7 = { |
| 3874 | N, I(Sse, em_mov), N, N, |
| 3875 | }; |
| 3876 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3877 | static const struct escape escape_d9 = { { |
| 3878 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3879 | }, { |
| 3880 | /* 0xC0 - 0xC7 */ |
| 3881 | N, N, N, N, N, N, N, N, |
| 3882 | /* 0xC8 - 0xCF */ |
| 3883 | N, N, N, N, N, N, N, N, |
| 3884 | /* 0xD0 - 0xC7 */ |
| 3885 | N, N, N, N, N, N, N, N, |
| 3886 | /* 0xD8 - 0xDF */ |
| 3887 | N, N, N, N, N, N, N, N, |
| 3888 | /* 0xE0 - 0xE7 */ |
| 3889 | N, N, N, N, N, N, N, N, |
| 3890 | /* 0xE8 - 0xEF */ |
| 3891 | N, N, N, N, N, N, N, N, |
| 3892 | /* 0xF0 - 0xF7 */ |
| 3893 | N, N, N, N, N, N, N, N, |
| 3894 | /* 0xF8 - 0xFF */ |
| 3895 | N, N, N, N, N, N, N, N, |
| 3896 | } }; |
| 3897 | |
| 3898 | static const struct escape escape_db = { { |
| 3899 | N, N, N, N, N, N, N, N, |
| 3900 | }, { |
| 3901 | /* 0xC0 - 0xC7 */ |
| 3902 | N, N, N, N, N, N, N, N, |
| 3903 | /* 0xC8 - 0xCF */ |
| 3904 | N, N, N, N, N, N, N, N, |
| 3905 | /* 0xD0 - 0xC7 */ |
| 3906 | N, N, N, N, N, N, N, N, |
| 3907 | /* 0xD8 - 0xDF */ |
| 3908 | N, N, N, N, N, N, N, N, |
| 3909 | /* 0xE0 - 0xE7 */ |
| 3910 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3911 | /* 0xE8 - 0xEF */ |
| 3912 | N, N, N, N, N, N, N, N, |
| 3913 | /* 0xF0 - 0xF7 */ |
| 3914 | N, N, N, N, N, N, N, N, |
| 3915 | /* 0xF8 - 0xFF */ |
| 3916 | N, N, N, N, N, N, N, N, |
| 3917 | } }; |
| 3918 | |
| 3919 | static const struct escape escape_dd = { { |
| 3920 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3921 | }, { |
| 3922 | /* 0xC0 - 0xC7 */ |
| 3923 | N, N, N, N, N, N, N, N, |
| 3924 | /* 0xC8 - 0xCF */ |
| 3925 | N, N, N, N, N, N, N, N, |
| 3926 | /* 0xD0 - 0xC7 */ |
| 3927 | N, N, N, N, N, N, N, N, |
| 3928 | /* 0xD8 - 0xDF */ |
| 3929 | N, N, N, N, N, N, N, N, |
| 3930 | /* 0xE0 - 0xE7 */ |
| 3931 | N, N, N, N, N, N, N, N, |
| 3932 | /* 0xE8 - 0xEF */ |
| 3933 | N, N, N, N, N, N, N, N, |
| 3934 | /* 0xF0 - 0xF7 */ |
| 3935 | N, N, N, N, N, N, N, N, |
| 3936 | /* 0xF8 - 0xFF */ |
| 3937 | N, N, N, N, N, N, N, N, |
| 3938 | } }; |
| 3939 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3940 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3941 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3942 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3943 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3944 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3945 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3946 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3947 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3948 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3949 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3950 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3951 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3952 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3953 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3954 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3955 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3956 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3957 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3958 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3959 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3960 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3961 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3962 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3963 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3964 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3965 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3966 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3967 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3968 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3969 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3970 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3971 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3972 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3973 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3974 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3975 | N, N, N, N, |
| 3976 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3977 | I(SrcImm | Mov | Stack, em_push), |
| 3978 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3979 | I(SrcImmByte | Mov | Stack, em_push), |
| 3980 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3981 | 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] | 3982 | 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] | 3983 | /* 0x70 - 0x7F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 3984 | X16(D(SrcImmByte | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3985 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3986 | G(ByteOp | DstMem | SrcImm, group1), |
| 3987 | G(DstMem | SrcImm, group1), |
| 3988 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3989 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3990 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3991 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3992 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3993 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3994 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3995 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3996 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3997 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3998 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3999 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4000 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4001 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 4002 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 4003 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 4004 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 4005 | II(ImplicitOps | Stack, em_popf, popf), |
| 4006 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4007 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4008 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4009 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4010 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4011 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4012 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 4013 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4014 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 4015 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Nadav Amit | 5aca372 | 2014-11-02 11:54:50 +0200 | [diff] [blame] | 4016 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4017 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 4018 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4019 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4020 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4021 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4022 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4023 | I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm), |
| 4024 | I(ImplicitOps | NearBranch, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4025 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 4026 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 4027 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4028 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 4029 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 4030 | I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm), |
| 4031 | I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4032 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4033 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4034 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 4035 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 4036 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 4037 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 4038 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 4039 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4040 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4041 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4042 | 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] | 4043 | /* 0xE0 - 0xE7 */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4044 | X3(I(SrcImmByte | NearBranch, em_loop)), |
| 4045 | I(SrcImmByte | NearBranch, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4046 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 4047 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4048 | /* 0xE8 - 0xEF */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4049 | I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch), |
| 4050 | I(SrcImmFAddr | No64, em_jmp_far), |
| 4051 | D(SrcImmByte | ImplicitOps | NearBranch), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4052 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 4053 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4054 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4055 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4056 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 4057 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4058 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 4059 | D(ImplicitOps), D(ImplicitOps), |
| 4060 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4061 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 4062 | }; |
| 4063 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 4064 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4065 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4066 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4067 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4068 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4069 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4070 | N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4071 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4072 | N, N, N, N, N, N, N, N, |
Nadav Amit | 3f6f148 | 2014-10-13 13:04:14 +0300 | [diff] [blame] | 4073 | D(ImplicitOps | ModRM | SrcMem | NoAccess), |
| 4074 | N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4075 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 4076 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 4077 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 4078 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 4079 | check_cr_write), |
| 4080 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 4081 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4082 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 4083 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 4084 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
Paolo Bonzini | d5b7706 | 2014-07-14 12:54:48 +0200 | [diff] [blame] | 4085 | N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4086 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4087 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4088 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4089 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4090 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4091 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4092 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 4093 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4094 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4095 | N, N, N, N, N, N, N, N, |
| 4096 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4097 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4098 | /* 0x50 - 0x5F */ |
| 4099 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4100 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4101 | N, N, N, N, |
| 4102 | N, N, N, N, |
| 4103 | N, N, N, N, |
| 4104 | 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] | 4105 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4106 | N, N, N, N, |
| 4107 | N, N, N, N, |
| 4108 | N, N, N, N, |
| 4109 | 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] | 4110 | /* 0x80 - 0x8F */ |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4111 | X16(D(SrcImm | NearBranch)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4112 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4113 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4114 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4115 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4116 | II(ImplicitOps, em_cpuid, cpuid), |
| 4117 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4118 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4119 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4120 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4121 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4122 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4123 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4124 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4125 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Nadav Amit | 13e457e | 2014-10-13 13:04:13 +0300 | [diff] [blame] | 4126 | GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4127 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 4128 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4129 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4130 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4131 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4132 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4133 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4134 | /* 0xB8 - 0xBF */ |
| 4135 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4136 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4137 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 4138 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4139 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4140 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 4141 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4142 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4143 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4144 | /* 0xC8 - 0xCF */ |
| 4145 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4146 | /* 0xD0 - 0xDF */ |
| 4147 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4148 | /* 0xE0 - 0xEF */ |
Alex Williamson | 0a37027 | 2014-07-11 11:56:31 -0600 | [diff] [blame] | 4149 | N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), |
| 4150 | N, N, N, N, N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4151 | /* 0xF0 - 0xFF */ |
| 4152 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4153 | }; |
| 4154 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4155 | static const struct gprefix three_byte_0f_38_f0 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4156 | I(DstReg | SrcMem | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4157 | }; |
| 4158 | |
| 4159 | static const struct gprefix three_byte_0f_38_f1 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 4160 | I(DstMem | SrcReg | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4161 | }; |
| 4162 | |
| 4163 | /* |
| 4164 | * Insns below are selected by the prefix which indexed by the third opcode |
| 4165 | * byte. |
| 4166 | */ |
| 4167 | static const struct opcode opcode_map_0f_38[256] = { |
| 4168 | /* 0x00 - 0x7f */ |
| 4169 | 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] | 4170 | /* 0x80 - 0xef */ |
| 4171 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 4172 | /* 0xf0 - 0xf1 */ |
| 4173 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0), |
| 4174 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1), |
| 4175 | /* 0xf2 - 0xff */ |
| 4176 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4177 | }; |
| 4178 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4179 | #undef D |
| 4180 | #undef N |
| 4181 | #undef G |
| 4182 | #undef GD |
| 4183 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4184 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4185 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4186 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4187 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4188 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4189 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4190 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4191 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4192 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4193 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4194 | { |
| 4195 | unsigned size; |
| 4196 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4197 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4198 | if (size == 8) |
| 4199 | size = 4; |
| 4200 | return size; |
| 4201 | } |
| 4202 | |
| 4203 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4204 | unsigned size, bool sign_extension) |
| 4205 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4206 | int rc = X86EMUL_CONTINUE; |
| 4207 | |
| 4208 | op->type = OP_IMM; |
| 4209 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4210 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4211 | /* NB. Immediates are sign-extended as necessary. */ |
| 4212 | switch (op->bytes) { |
| 4213 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4214 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4215 | break; |
| 4216 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4217 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4218 | break; |
| 4219 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4220 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4221 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4222 | case 8: |
| 4223 | op->val = insn_fetch(s64, ctxt); |
| 4224 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4225 | } |
| 4226 | if (!sign_extension) { |
| 4227 | switch (op->bytes) { |
| 4228 | case 1: |
| 4229 | op->val &= 0xff; |
| 4230 | break; |
| 4231 | case 2: |
| 4232 | op->val &= 0xffff; |
| 4233 | break; |
| 4234 | case 4: |
| 4235 | op->val &= 0xffffffff; |
| 4236 | break; |
| 4237 | } |
| 4238 | } |
| 4239 | done: |
| 4240 | return rc; |
| 4241 | } |
| 4242 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4243 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4244 | unsigned d) |
| 4245 | { |
| 4246 | int rc = X86EMUL_CONTINUE; |
| 4247 | |
| 4248 | switch (d) { |
| 4249 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4250 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4251 | break; |
| 4252 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4253 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4254 | break; |
| 4255 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4256 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4257 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4258 | *op = ctxt->memop; |
| 4259 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4260 | if (ctxt->d & BitOp) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4261 | fetch_bit_operand(ctxt); |
| 4262 | op->orig_val = op->val; |
| 4263 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4264 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4265 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4266 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4267 | case OpAcc: |
| 4268 | op->type = OP_REG; |
| 4269 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4270 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4271 | fetch_register_operand(op); |
| 4272 | op->orig_val = op->val; |
| 4273 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4274 | case OpAccLo: |
| 4275 | op->type = OP_REG; |
| 4276 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4277 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4278 | fetch_register_operand(op); |
| 4279 | op->orig_val = op->val; |
| 4280 | break; |
| 4281 | case OpAccHi: |
| 4282 | if (ctxt->d & ByteOp) { |
| 4283 | op->type = OP_NONE; |
| 4284 | break; |
| 4285 | } |
| 4286 | op->type = OP_REG; |
| 4287 | op->bytes = ctxt->op_bytes; |
| 4288 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4289 | fetch_register_operand(op); |
| 4290 | op->orig_val = op->val; |
| 4291 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4292 | case OpDI: |
| 4293 | op->type = OP_MEM; |
| 4294 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4295 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4296 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4297 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4298 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4299 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4300 | break; |
| 4301 | case OpDX: |
| 4302 | op->type = OP_REG; |
| 4303 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4304 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4305 | fetch_register_operand(op); |
| 4306 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4307 | case OpCL: |
| 4308 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4309 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4310 | break; |
| 4311 | case OpImmByte: |
| 4312 | rc = decode_imm(ctxt, op, 1, true); |
| 4313 | break; |
| 4314 | case OpOne: |
| 4315 | op->bytes = 1; |
| 4316 | op->val = 1; |
| 4317 | break; |
| 4318 | case OpImm: |
| 4319 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4320 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4321 | case OpImm64: |
| 4322 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4323 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4324 | case OpMem8: |
| 4325 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4326 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4327 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4328 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4329 | fetch_register_operand(&ctxt->memop); |
| 4330 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4331 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4332 | case OpMem16: |
| 4333 | ctxt->memop.bytes = 2; |
| 4334 | goto mem_common; |
| 4335 | case OpMem32: |
| 4336 | ctxt->memop.bytes = 4; |
| 4337 | goto mem_common; |
| 4338 | case OpImmU16: |
| 4339 | rc = decode_imm(ctxt, op, 2, false); |
| 4340 | break; |
| 4341 | case OpImmU: |
| 4342 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4343 | break; |
| 4344 | case OpSI: |
| 4345 | op->type = OP_MEM; |
| 4346 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4347 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4348 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4349 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4350 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4351 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4352 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4353 | case OpXLat: |
| 4354 | op->type = OP_MEM; |
| 4355 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4356 | op->addr.mem.ea = |
| 4357 | register_address(ctxt, |
| 4358 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4359 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4360 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4361 | op->val = 0; |
| 4362 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4363 | case OpImmFAddr: |
| 4364 | op->type = OP_IMM; |
| 4365 | op->addr.mem.ea = ctxt->_eip; |
| 4366 | op->bytes = ctxt->op_bytes + 2; |
| 4367 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4368 | break; |
| 4369 | case OpMemFAddr: |
| 4370 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4371 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4372 | case OpES: |
| 4373 | op->val = VCPU_SREG_ES; |
| 4374 | break; |
| 4375 | case OpCS: |
| 4376 | op->val = VCPU_SREG_CS; |
| 4377 | break; |
| 4378 | case OpSS: |
| 4379 | op->val = VCPU_SREG_SS; |
| 4380 | break; |
| 4381 | case OpDS: |
| 4382 | op->val = VCPU_SREG_DS; |
| 4383 | break; |
| 4384 | case OpFS: |
| 4385 | op->val = VCPU_SREG_FS; |
| 4386 | break; |
| 4387 | case OpGS: |
| 4388 | op->val = VCPU_SREG_GS; |
| 4389 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4390 | case OpImplicit: |
| 4391 | /* Special instructions do their own operand decoding. */ |
| 4392 | default: |
| 4393 | op->type = OP_NONE; /* Disable writeback. */ |
| 4394 | break; |
| 4395 | } |
| 4396 | |
| 4397 | done: |
| 4398 | return rc; |
| 4399 | } |
| 4400 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4401 | 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] | 4402 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4403 | int rc = X86EMUL_CONTINUE; |
| 4404 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4405 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4406 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4407 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4408 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4409 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4410 | ctxt->memop.type = OP_NONE; |
| 4411 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4412 | ctxt->_eip = ctxt->eip; |
Paolo Bonzini | 17052f1 | 2014-05-06 16:33:01 +0200 | [diff] [blame] | 4413 | ctxt->fetch.ptr = ctxt->fetch.data; |
| 4414 | ctxt->fetch.end = ctxt->fetch.data + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4415 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4416 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4417 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4418 | else { |
Paolo Bonzini | 9506d57 | 2014-05-06 13:05:25 +0200 | [diff] [blame] | 4419 | rc = __do_insn_fetch_bytes(ctxt, 1); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame] | 4420 | if (rc != X86EMUL_CONTINUE) |
| 4421 | return rc; |
| 4422 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4423 | |
| 4424 | switch (mode) { |
| 4425 | case X86EMUL_MODE_REAL: |
| 4426 | case X86EMUL_MODE_VM86: |
| 4427 | case X86EMUL_MODE_PROT16: |
| 4428 | def_op_bytes = def_ad_bytes = 2; |
| 4429 | break; |
| 4430 | case X86EMUL_MODE_PROT32: |
| 4431 | def_op_bytes = def_ad_bytes = 4; |
| 4432 | break; |
| 4433 | #ifdef CONFIG_X86_64 |
| 4434 | case X86EMUL_MODE_PROT64: |
| 4435 | def_op_bytes = 4; |
| 4436 | def_ad_bytes = 8; |
| 4437 | break; |
| 4438 | #endif |
| 4439 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4440 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4441 | } |
| 4442 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4443 | ctxt->op_bytes = def_op_bytes; |
| 4444 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4445 | |
| 4446 | /* Legacy prefixes. */ |
| 4447 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4448 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4449 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4450 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4451 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4452 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4453 | break; |
| 4454 | case 0x67: /* address-size override */ |
| 4455 | if (mode == X86EMUL_MODE_PROT64) |
| 4456 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4457 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4458 | else |
| 4459 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4460 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4461 | break; |
| 4462 | case 0x26: /* ES override */ |
| 4463 | case 0x2e: /* CS override */ |
| 4464 | case 0x36: /* SS override */ |
| 4465 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4466 | has_seg_override = true; |
| 4467 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4468 | break; |
| 4469 | case 0x64: /* FS override */ |
| 4470 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4471 | has_seg_override = true; |
| 4472 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4473 | break; |
| 4474 | case 0x40 ... 0x4f: /* REX */ |
| 4475 | if (mode != X86EMUL_MODE_PROT64) |
| 4476 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4477 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4478 | continue; |
| 4479 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4480 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4481 | break; |
| 4482 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4483 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4484 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4485 | break; |
| 4486 | default: |
| 4487 | goto done_prefixes; |
| 4488 | } |
| 4489 | |
| 4490 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4491 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4492 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | done_prefixes: |
| 4496 | |
| 4497 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4498 | if (ctxt->rex_prefix & 8) |
| 4499 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4500 | |
| 4501 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4502 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4503 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4504 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4505 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4506 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4507 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4508 | |
| 4509 | /* 0F_38 opcode map */ |
| 4510 | if (ctxt->b == 0x38) { |
| 4511 | ctxt->opcode_len = 3; |
| 4512 | ctxt->b = insn_fetch(u8, ctxt); |
| 4513 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4514 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4515 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4516 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4517 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4518 | if (ctxt->d & ModRM) |
| 4519 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4520 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4521 | /* vex-prefix instructions are not implemented */ |
| 4522 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
| 4523 | (mode == X86EMUL_MODE_PROT64 || |
| 4524 | (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) { |
| 4525 | ctxt->d = NotImpl; |
| 4526 | } |
| 4527 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4528 | while (ctxt->d & GroupMask) { |
| 4529 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4530 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4531 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4532 | opcode = opcode.u.group[goffset]; |
| 4533 | break; |
| 4534 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4535 | goffset = (ctxt->modrm >> 3) & 7; |
| 4536 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4537 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4538 | else |
| 4539 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4540 | break; |
| 4541 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4542 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4543 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4544 | break; |
| 4545 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4546 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4547 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4548 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4549 | switch (simd_prefix) { |
| 4550 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4551 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4552 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4553 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4554 | } |
| 4555 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4556 | case Escape: |
| 4557 | if (ctxt->modrm > 0xbf) |
| 4558 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4559 | else |
| 4560 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4561 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4562 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4563 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4564 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4565 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4566 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4567 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 4570 | /* Unrecognised? */ |
| 4571 | if (ctxt->d == 0) |
| 4572 | return EMULATION_FAILED; |
| 4573 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4574 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4575 | |
Nadav Amit | 3a6095a | 2014-08-13 16:50:13 +0300 | [diff] [blame] | 4576 | if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) |
| 4577 | return EMULATION_FAILED; |
| 4578 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4579 | if (unlikely(ctxt->d & |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4580 | (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch))) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4581 | /* |
| 4582 | * These are copied unconditionally here, and checked unconditionally |
| 4583 | * in x86_emulate_insn. |
| 4584 | */ |
| 4585 | ctxt->check_perm = opcode.check_perm; |
| 4586 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4587 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4588 | if (ctxt->d & NotImpl) |
| 4589 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4590 | |
Nadav Amit | 58b7075 | 2014-10-24 11:35:09 +0300 | [diff] [blame] | 4591 | if (mode == X86EMUL_MODE_PROT64) { |
| 4592 | if (ctxt->op_bytes == 4 && (ctxt->d & Stack)) |
| 4593 | ctxt->op_bytes = 8; |
| 4594 | else if (ctxt->d & NearBranch) |
| 4595 | ctxt->op_bytes = 8; |
| 4596 | } |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4597 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4598 | if (ctxt->d & Op3264) { |
| 4599 | if (mode == X86EMUL_MODE_PROT64) |
| 4600 | ctxt->op_bytes = 8; |
| 4601 | else |
| 4602 | ctxt->op_bytes = 4; |
| 4603 | } |
| 4604 | |
| 4605 | if (ctxt->d & Sse) |
| 4606 | ctxt->op_bytes = 16; |
| 4607 | else if (ctxt->d & Mmx) |
| 4608 | ctxt->op_bytes = 8; |
| 4609 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4610 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4611 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4612 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4613 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4614 | if (!has_seg_override) { |
| 4615 | has_seg_override = true; |
| 4616 | ctxt->seg_override = ctxt->modrm_seg; |
| 4617 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4618 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4619 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4620 | if (rc != X86EMUL_CONTINUE) |
| 4621 | goto done; |
| 4622 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4623 | if (!has_seg_override) |
| 4624 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4625 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4626 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4627 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4628 | /* |
| 4629 | * Decode and fetch the source operand: register, memory |
| 4630 | * or immediate. |
| 4631 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4632 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4633 | if (rc != X86EMUL_CONTINUE) |
| 4634 | goto done; |
| 4635 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4636 | /* |
| 4637 | * Decode and fetch the second source operand: register, memory |
| 4638 | * or immediate. |
| 4639 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4640 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4641 | if (rc != X86EMUL_CONTINUE) |
| 4642 | goto done; |
| 4643 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4644 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4645 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4646 | |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 4647 | if (ctxt->rip_relative) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4648 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4649 | |
Paolo Bonzini | a430c91 | 2014-10-23 14:54:14 +0200 | [diff] [blame] | 4650 | done: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4651 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4652 | } |
| 4653 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4654 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4655 | { |
| 4656 | return ctxt->d & PageTable; |
| 4657 | } |
| 4658 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4659 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4660 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4661 | /* The second termination condition only applies for REPE |
| 4662 | * and REPNE. Test if the repeat string operation prefix is |
| 4663 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4664 | * corresponding termination condition according to: |
| 4665 | * - if REPE/REPZ and ZF = 0 then done |
| 4666 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4667 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4668 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4669 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4670 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4671 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4672 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4673 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4674 | return true; |
| 4675 | |
| 4676 | return false; |
| 4677 | } |
| 4678 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4679 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4680 | { |
| 4681 | bool fault = false; |
| 4682 | |
| 4683 | ctxt->ops->get_fpu(ctxt); |
| 4684 | asm volatile("1: fwait \n\t" |
| 4685 | "2: \n\t" |
| 4686 | ".pushsection .fixup,\"ax\" \n\t" |
| 4687 | "3: \n\t" |
| 4688 | "movb $1, %[fault] \n\t" |
| 4689 | "jmp 2b \n\t" |
| 4690 | ".popsection \n\t" |
| 4691 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4692 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4693 | ctxt->ops->put_fpu(ctxt); |
| 4694 | |
| 4695 | if (unlikely(fault)) |
| 4696 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4697 | |
| 4698 | return X86EMUL_CONTINUE; |
| 4699 | } |
| 4700 | |
| 4701 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4702 | struct operand *op) |
| 4703 | { |
| 4704 | if (op->type == OP_MM) |
| 4705 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4706 | } |
| 4707 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4708 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4709 | { |
| 4710 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4711 | if (!(ctxt->d & ByteOp)) |
| 4712 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4713 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4714 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4715 | [fastop]"+S"(fop) |
| 4716 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4717 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4718 | if (!fop) /* exception is returned in fop variable */ |
| 4719 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4720 | return X86EMUL_CONTINUE; |
| 4721 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4722 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4723 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 4724 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4725 | memset(&ctxt->rip_relative, 0, |
| 4726 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4727 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4728 | ctxt->io_read.pos = 0; |
| 4729 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4730 | ctxt->mem_read.end = 0; |
| 4731 | } |
| 4732 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4733 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4734 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4735 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4736 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4737 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4738 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4739 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4740 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4741 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4742 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4743 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4744 | goto done; |
| 4745 | } |
| 4746 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4747 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4748 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4749 | goto done; |
| 4750 | } |
| 4751 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4752 | if (unlikely(ctxt->d & |
| 4753 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 4754 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4755 | (ctxt->d & Undefined)) { |
| 4756 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4757 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4758 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4759 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4760 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4761 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 4762 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4763 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4764 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4765 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4766 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 4767 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4768 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4769 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4770 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4771 | if (ctxt->d & Mmx) { |
| 4772 | rc = flush_pending_x87_faults(ctxt); |
| 4773 | if (rc != X86EMUL_CONTINUE) |
| 4774 | goto done; |
| 4775 | /* |
| 4776 | * Now that we know the fpu is exception safe, we can fetch |
| 4777 | * operands from it. |
| 4778 | */ |
| 4779 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4780 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4781 | if (!(ctxt->d & Mov)) |
| 4782 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4783 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4784 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4785 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4786 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4787 | X86_ICPT_PRE_EXCEPT); |
| 4788 | if (rc != X86EMUL_CONTINUE) |
| 4789 | goto done; |
| 4790 | } |
| 4791 | |
| 4792 | /* Privileged instruction can be executed only in CPL=0 */ |
| 4793 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Nadav Amit | 68efa76 | 2014-06-18 17:19:35 +0300 | [diff] [blame] | 4794 | if (ctxt->d & PrivUD) |
| 4795 | rc = emulate_ud(ctxt); |
| 4796 | else |
| 4797 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4798 | goto done; |
| 4799 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4800 | |
| 4801 | /* Instruction can only be executed in protected mode */ |
| 4802 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 4803 | rc = emulate_ud(ctxt); |
| 4804 | goto done; |
| 4805 | } |
| 4806 | |
| 4807 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4808 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4809 | rc = ctxt->check_perm(ctxt); |
| 4810 | if (rc != X86EMUL_CONTINUE) |
| 4811 | goto done; |
| 4812 | } |
| 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_POST_EXCEPT); |
| 4817 | if (rc != X86EMUL_CONTINUE) |
| 4818 | goto done; |
| 4819 | } |
| 4820 | |
| 4821 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 4822 | /* All REP prefixes have the same first termination condition */ |
| 4823 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
| 4824 | ctxt->eip = ctxt->_eip; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4825 | ctxt->eflags &= ~EFLG_RF; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4826 | goto done; |
| 4827 | } |
| 4828 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4829 | } |
| 4830 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4831 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4832 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4833 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4834 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4835 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4836 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4837 | } |
| 4838 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4839 | if (ctxt->src2.type == OP_MEM) { |
| 4840 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4841 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4842 | if (rc != X86EMUL_CONTINUE) |
| 4843 | goto done; |
| 4844 | } |
| 4845 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4846 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4847 | goto special_insn; |
| 4848 | |
| 4849 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4850 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4851 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4852 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4853 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4854 | if (rc != X86EMUL_CONTINUE) |
| 4855 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4856 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4857 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4858 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4859 | special_insn: |
| 4860 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4861 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4862 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4863 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4864 | if (rc != X86EMUL_CONTINUE) |
| 4865 | goto done; |
| 4866 | } |
| 4867 | |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 4868 | if (ctxt->rep_prefix && (ctxt->d & String)) |
| 4869 | ctxt->eflags |= EFLG_RF; |
| 4870 | else |
| 4871 | ctxt->eflags &= ~EFLG_RF; |
Nadav Amit | 4467c3f | 2014-07-21 14:37:29 +0300 | [diff] [blame] | 4872 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4873 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4874 | if (ctxt->d & Fastop) { |
| 4875 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4876 | rc = fastop(ctxt, fop); |
| 4877 | if (rc != X86EMUL_CONTINUE) |
| 4878 | goto done; |
| 4879 | goto writeback; |
| 4880 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4881 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4882 | if (rc != X86EMUL_CONTINUE) |
| 4883 | goto done; |
| 4884 | goto writeback; |
| 4885 | } |
| 4886 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4887 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4888 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4889 | else if (ctxt->opcode_len == 3) |
| 4890 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4891 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4892 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4893 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4894 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4895 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4896 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4897 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4898 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4899 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4900 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4901 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4902 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4903 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4904 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4905 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4906 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 4907 | ctxt->dst.type = OP_NONE; |
| 4908 | else |
| 4909 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4910 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4911 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4912 | switch (ctxt->op_bytes) { |
| 4913 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4914 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4915 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4916 | } |
| 4917 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4918 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4919 | rc = emulate_int(ctxt, 3); |
| 4920 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4921 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4922 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4923 | break; |
| 4924 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4925 | if (ctxt->eflags & EFLG_OF) |
| 4926 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4927 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4928 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4929 | case 0xeb: /* jmp rel short */ |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 4930 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4931 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4932 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4933 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4934 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4935 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4936 | case 0xf5: /* cmc */ |
| 4937 | /* complement carry flag from eflags reg */ |
| 4938 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4939 | break; |
| 4940 | case 0xf8: /* clc */ |
| 4941 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4942 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4943 | case 0xf9: /* stc */ |
| 4944 | ctxt->eflags |= EFLG_CF; |
| 4945 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4946 | case 0xfc: /* cld */ |
| 4947 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4948 | break; |
| 4949 | case 0xfd: /* std */ |
| 4950 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4951 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4952 | default: |
| 4953 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4954 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4955 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4956 | if (rc != X86EMUL_CONTINUE) |
| 4957 | goto done; |
| 4958 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4959 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 4960 | if (ctxt->d & SrcWrite) { |
| 4961 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 4962 | rc = writeback(ctxt, &ctxt->src); |
| 4963 | if (rc != X86EMUL_CONTINUE) |
| 4964 | goto done; |
| 4965 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 4966 | if (!(ctxt->d & NoWrite)) { |
| 4967 | rc = writeback(ctxt, &ctxt->dst); |
| 4968 | if (rc != X86EMUL_CONTINUE) |
| 4969 | goto done; |
| 4970 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4971 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4972 | /* |
| 4973 | * restore dst type in case the decoding will be reused |
| 4974 | * (happens for string instruction ) |
| 4975 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4976 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4977 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4978 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4979 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4980 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4981 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4982 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4983 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4984 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4985 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4986 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4987 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4988 | count = ctxt->src.count; |
| 4989 | else |
| 4990 | count = ctxt->dst.count; |
| 4991 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4992 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4993 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4994 | if (!string_insn_completed(ctxt)) { |
| 4995 | /* |
| 4996 | * Re-enter guest when pio read ahead buffer is empty |
| 4997 | * or, if it is not used, after each 1024 iteration. |
| 4998 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4999 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5000 | (r->end == 0 || r->end != r->pos)) { |
| 5001 | /* |
| 5002 | * Reset read cache. Usually happens before |
| 5003 | * decode, but since instruction is restarted |
| 5004 | * we have to do it here. |
| 5005 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5006 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5007 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5008 | return EMULATION_RESTART; |
| 5009 | } |
| 5010 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 5011 | } |
Nadav Amit | b9a1ecb | 2014-07-24 14:51:23 +0300 | [diff] [blame] | 5012 | ctxt->eflags &= ~EFLG_RF; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5013 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5014 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5015 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5016 | |
| 5017 | done: |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5018 | if (rc == X86EMUL_PROPAGATE_FAULT) { |
| 5019 | WARN_ON(ctxt->exception.vector > 0x1f); |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 5020 | ctxt->have_exception = true; |
Paolo Bonzini | e0ad0b4 | 2014-08-20 10:08:23 +0200 | [diff] [blame] | 5021 | } |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 5022 | if (rc == X86EMUL_INTERCEPTED) |
| 5023 | return EMULATION_INTERCEPTED; |
| 5024 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5025 | if (rc == X86EMUL_CONTINUE) |
| 5026 | writeback_registers(ctxt); |
| 5027 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 5028 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5029 | |
| 5030 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5031 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5032 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 5033 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5034 | break; |
| 5035 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5036 | case 0x0d: /* GrpP (prefetch) */ |
| 5037 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 5038 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5039 | break; |
| 5040 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5041 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5042 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5043 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5044 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5045 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5046 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 5047 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 5048 | ctxt->dst.val = ctxt->src.val; |
| 5049 | else if (ctxt->mode != X86EMUL_MODE_PROT64 || |
| 5050 | ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5051 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5052 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 5053 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5054 | if (test_cc(ctxt->b, ctxt->eflags)) |
Nadav Amit | 234f3ce | 2014-09-18 22:39:38 +0300 | [diff] [blame] | 5055 | rc = jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 5056 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5057 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5058 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 5059 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5060 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5061 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5062 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5063 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5064 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5065 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5066 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 5067 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5068 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5069 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 5070 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 5071 | ctxt->dst.bytes = ctxt->op_bytes; |
Nadav Amit | 3b32004 | 2014-06-02 18:34:08 +0300 | [diff] [blame] | 5072 | ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val : |
| 5073 | (u32) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 5074 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 5075 | default: |
| 5076 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5077 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5078 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 5079 | threebyte_insn: |
| 5080 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 5081 | if (rc != X86EMUL_CONTINUE) |
| 5082 | goto done; |
| 5083 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5084 | goto writeback; |
| 5085 | |
| 5086 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 5087 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5088 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 5089 | |
| 5090 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5091 | { |
| 5092 | invalidate_registers(ctxt); |
| 5093 | } |
| 5094 | |
| 5095 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 5096 | { |
| 5097 | writeback_registers(ctxt); |
| 5098 | } |