Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 29 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 30 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | /* |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
| 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 | a994554 | 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 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 167 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 168 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 169 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 170 | #define X2(x...) x, x |
| 171 | #define X3(x...) X2(x), x |
| 172 | #define X4(x...) X2(x), X2(x) |
| 173 | #define X5(x...) X4(x), x |
| 174 | #define X6(x...) X4(x), X2(x) |
| 175 | #define X7(x...) X4(x), X3(x) |
| 176 | #define X8(x...) X4(x), X4(x) |
| 177 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 178 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 179 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 180 | #define FASTOP_SIZE 8 |
| 181 | |
| 182 | /* |
| 183 | * fastop functions have a special calling convention: |
| 184 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 185 | * dst: rax (in/out) |
| 186 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 187 | * src2: rcx (in) |
| 188 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 189 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 190 | * |
| 191 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 192 | * different operand sizes can be reached by calculation, rather than a jump |
| 193 | * table (which would be bigger than the code). |
| 194 | * |
| 195 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 196 | * so they can't be called from C directly. |
| 197 | */ |
| 198 | |
| 199 | struct fastop; |
| 200 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 201 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 202 | u64 flags : 56; |
| 203 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 204 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 205 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 206 | const struct opcode *group; |
| 207 | const struct group_dual *gdual; |
| 208 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 209 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 210 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 211 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 212 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | struct group_dual { |
| 216 | struct opcode mod012[8]; |
| 217 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 218 | }; |
| 219 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 220 | struct gprefix { |
| 221 | struct opcode pfx_no; |
| 222 | struct opcode pfx_66; |
| 223 | struct opcode pfx_f2; |
| 224 | struct opcode pfx_f3; |
| 225 | }; |
| 226 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 227 | struct escape { |
| 228 | struct opcode op[8]; |
| 229 | struct opcode high[64]; |
| 230 | }; |
| 231 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 233 | #define EFLG_ID (1<<21) |
| 234 | #define EFLG_VIP (1<<20) |
| 235 | #define EFLG_VIF (1<<19) |
| 236 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 237 | #define EFLG_VM (1<<17) |
| 238 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 239 | #define EFLG_IOPL (3<<12) |
| 240 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 241 | #define EFLG_OF (1<<11) |
| 242 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 243 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 244 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 245 | #define EFLG_SF (1<<7) |
| 246 | #define EFLG_ZF (1<<6) |
| 247 | #define EFLG_AF (1<<4) |
| 248 | #define EFLG_PF (1<<2) |
| 249 | #define EFLG_CF (1<<0) |
| 250 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 251 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 252 | #define EFLG_RESERVED_ONE_MASK 2 |
| 253 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 254 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 255 | { |
| 256 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 257 | ctxt->regs_valid |= 1 << nr; |
| 258 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 259 | } |
| 260 | return ctxt->_regs[nr]; |
| 261 | } |
| 262 | |
| 263 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 264 | { |
| 265 | ctxt->regs_valid |= 1 << nr; |
| 266 | ctxt->regs_dirty |= 1 << nr; |
| 267 | return &ctxt->_regs[nr]; |
| 268 | } |
| 269 | |
| 270 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 271 | { |
| 272 | reg_read(ctxt, nr); |
| 273 | return reg_write(ctxt, nr); |
| 274 | } |
| 275 | |
| 276 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 277 | { |
| 278 | unsigned reg; |
| 279 | |
| 280 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 281 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 282 | } |
| 283 | |
| 284 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 285 | { |
| 286 | ctxt->regs_dirty = 0; |
| 287 | ctxt->regs_valid = 0; |
| 288 | } |
| 289 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 290 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 291 | * These EFLAGS bits are restored from saved value during emulation, and |
| 292 | * any changes are written back to the saved value after emulation. |
| 293 | */ |
| 294 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 295 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 296 | #ifdef CONFIG_X86_64 |
| 297 | #define ON64(x) x |
| 298 | #else |
| 299 | #define ON64(x) |
| 300 | #endif |
| 301 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 302 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 303 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 304 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 305 | #define FOP_RET "ret \n\t" |
| 306 | |
| 307 | #define FOP_START(op) \ |
| 308 | extern void em_##op(struct fastop *fake); \ |
| 309 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 310 | ".global em_" #op " \n\t" \ |
| 311 | FOP_ALIGN \ |
| 312 | "em_" #op ": \n\t" |
| 313 | |
| 314 | #define FOP_END \ |
| 315 | ".popsection") |
| 316 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 317 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 318 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 319 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 320 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 321 | |
| 322 | #define FOP1EEX(op, dst) \ |
| 323 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 324 | |
| 325 | #define FASTOP1(op) \ |
| 326 | FOP_START(op) \ |
| 327 | FOP1E(op##b, al) \ |
| 328 | FOP1E(op##w, ax) \ |
| 329 | FOP1E(op##l, eax) \ |
| 330 | ON64(FOP1E(op##q, rax)) \ |
| 331 | FOP_END |
| 332 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 333 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 334 | #define FASTOP1SRC2(op, name) \ |
| 335 | FOP_START(name) \ |
| 336 | FOP1E(op, cl) \ |
| 337 | FOP1E(op, cx) \ |
| 338 | FOP1E(op, ecx) \ |
| 339 | ON64(FOP1E(op, rcx)) \ |
| 340 | FOP_END |
| 341 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 342 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 343 | #define FASTOP1SRC2EX(op, name) \ |
| 344 | FOP_START(name) \ |
| 345 | FOP1EEX(op, cl) \ |
| 346 | FOP1EEX(op, cx) \ |
| 347 | FOP1EEX(op, ecx) \ |
| 348 | ON64(FOP1EEX(op, rcx)) \ |
| 349 | FOP_END |
| 350 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 351 | #define FOP2E(op, dst, src) \ |
| 352 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 353 | |
| 354 | #define FASTOP2(op) \ |
| 355 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 356 | FOP2E(op##b, al, dl) \ |
| 357 | FOP2E(op##w, ax, dx) \ |
| 358 | FOP2E(op##l, eax, edx) \ |
| 359 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 360 | FOP_END |
| 361 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 362 | /* 2 operand, word only */ |
| 363 | #define FASTOP2W(op) \ |
| 364 | FOP_START(op) \ |
| 365 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 366 | FOP2E(op##w, ax, dx) \ |
| 367 | FOP2E(op##l, eax, edx) \ |
| 368 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 369 | FOP_END |
| 370 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 371 | /* 2 operand, src is CL */ |
| 372 | #define FASTOP2CL(op) \ |
| 373 | FOP_START(op) \ |
| 374 | FOP2E(op##b, al, cl) \ |
| 375 | FOP2E(op##w, ax, cl) \ |
| 376 | FOP2E(op##l, eax, cl) \ |
| 377 | ON64(FOP2E(op##q, rax, cl)) \ |
| 378 | FOP_END |
| 379 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 380 | #define FOP3E(op, dst, src, src2) \ |
| 381 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 382 | |
| 383 | /* 3-operand, word-only, src2=cl */ |
| 384 | #define FASTOP3WCL(op) \ |
| 385 | FOP_START(op) \ |
| 386 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 387 | FOP3E(op##w, ax, dx, cl) \ |
| 388 | FOP3E(op##l, eax, edx, cl) \ |
| 389 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 390 | FOP_END |
| 391 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 392 | /* Special case for SETcc - 1 instruction per cc */ |
| 393 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 394 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 395 | asm(".global kvm_fastop_exception \n" |
| 396 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 397 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 398 | FOP_START(setcc) |
| 399 | FOP_SETCC(seto) |
| 400 | FOP_SETCC(setno) |
| 401 | FOP_SETCC(setc) |
| 402 | FOP_SETCC(setnc) |
| 403 | FOP_SETCC(setz) |
| 404 | FOP_SETCC(setnz) |
| 405 | FOP_SETCC(setbe) |
| 406 | FOP_SETCC(setnbe) |
| 407 | FOP_SETCC(sets) |
| 408 | FOP_SETCC(setns) |
| 409 | FOP_SETCC(setp) |
| 410 | FOP_SETCC(setnp) |
| 411 | FOP_SETCC(setl) |
| 412 | FOP_SETCC(setnl) |
| 413 | FOP_SETCC(setle) |
| 414 | FOP_SETCC(setnle) |
| 415 | FOP_END; |
| 416 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 417 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 418 | FOP_END; |
| 419 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 420 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 421 | enum x86_intercept intercept, |
| 422 | enum x86_intercept_stage stage) |
| 423 | { |
| 424 | struct x86_instruction_info info = { |
| 425 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 426 | .rep_prefix = ctxt->rep_prefix, |
| 427 | .modrm_mod = ctxt->modrm_mod, |
| 428 | .modrm_reg = ctxt->modrm_reg, |
| 429 | .modrm_rm = ctxt->modrm_rm, |
| 430 | .src_val = ctxt->src.val64, |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 431 | .dst_val = ctxt->dst.val64, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 432 | .src_bytes = ctxt->src.bytes, |
| 433 | .dst_bytes = ctxt->dst.bytes, |
| 434 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 435 | .next_rip = ctxt->eip, |
| 436 | }; |
| 437 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 438 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 441 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 442 | { |
| 443 | *dest = (*dest & ~mask) | (src & mask); |
| 444 | } |
| 445 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 446 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 447 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 448 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 451 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 452 | { |
| 453 | u16 sel; |
| 454 | struct desc_struct ss; |
| 455 | |
| 456 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 457 | return ~0UL; |
| 458 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 459 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 460 | } |
| 461 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 462 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 463 | { |
| 464 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 465 | } |
| 466 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 467 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 468 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 469 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 470 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 471 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 472 | return reg; |
| 473 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 474 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 478 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 479 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 480 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 483 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 484 | { |
| 485 | assign_masked(reg, *reg + inc, mask); |
| 486 | } |
| 487 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 488 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 489 | 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] | 490 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 491 | ulong mask; |
| 492 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 493 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 494 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 495 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 496 | mask = ad_mask(ctxt); |
| 497 | masked_increment(reg, mask, inc); |
| 498 | } |
| 499 | |
| 500 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 501 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 502 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 503 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 504 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 505 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 506 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 507 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 508 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 509 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 510 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 511 | { |
| 512 | u32 limit = get_desc_limit(desc); |
| 513 | |
| 514 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 515 | } |
| 516 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 517 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 518 | { |
| 519 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 520 | return 0; |
| 521 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 522 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 523 | } |
| 524 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 525 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 526 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 527 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 528 | ctxt->exception.vector = vec; |
| 529 | ctxt->exception.error_code = error; |
| 530 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 531 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 532 | } |
| 533 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 534 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 535 | { |
| 536 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 537 | } |
| 538 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 539 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 540 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 541 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 542 | } |
| 543 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 544 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 545 | { |
| 546 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 547 | } |
| 548 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 549 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 550 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 551 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 552 | } |
| 553 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 554 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 555 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 556 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 557 | } |
| 558 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 559 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 560 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 561 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 562 | } |
| 563 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 564 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 565 | { |
| 566 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 567 | } |
| 568 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 569 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 570 | { |
| 571 | u16 selector; |
| 572 | struct desc_struct desc; |
| 573 | |
| 574 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 575 | return selector; |
| 576 | } |
| 577 | |
| 578 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 579 | unsigned seg) |
| 580 | { |
| 581 | u16 dummy; |
| 582 | u32 base3; |
| 583 | struct desc_struct desc; |
| 584 | |
| 585 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 586 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 587 | } |
| 588 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 589 | /* |
| 590 | * x86 defines three classes of vector instructions: explicitly |
| 591 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 592 | * depending on whether they're AVX encoded or not. |
| 593 | * |
| 594 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 595 | * subject to the same check. |
| 596 | */ |
| 597 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 598 | { |
| 599 | if (likely(size < 16)) |
| 600 | return false; |
| 601 | |
| 602 | if (ctxt->d & Aligned) |
| 603 | return true; |
| 604 | else if (ctxt->d & Unaligned) |
| 605 | return false; |
| 606 | else if (ctxt->d & Avx) |
| 607 | return false; |
| 608 | else |
| 609 | return true; |
| 610 | } |
| 611 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 612 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 613 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 614 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 615 | ulong *linear) |
| 616 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 617 | struct desc_struct desc; |
| 618 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 619 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 620 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 621 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 622 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 623 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 624 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 625 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 626 | case X86EMUL_MODE_PROT64: |
| 627 | if (((signed long)la << 16) >> 16 != la) |
| 628 | return emulate_gp(ctxt, 0); |
| 629 | break; |
| 630 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 631 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 632 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 633 | if (!usable) |
| 634 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 635 | /* code segment in protected mode or read-only data segment */ |
| 636 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 637 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 638 | goto bad; |
| 639 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 640 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 641 | goto bad; |
| 642 | lim = desc_limit_scaled(&desc); |
| 643 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 644 | /* expand-up segment */ |
| 645 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 646 | goto bad; |
| 647 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 648 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 649 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 650 | goto bad; |
| 651 | lim = desc.d ? 0xffffffff : 0xffff; |
| 652 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 653 | goto bad; |
| 654 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 655 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 656 | if (!(desc.type & 8)) { |
| 657 | /* data segment */ |
| 658 | if (cpl > desc.dpl) |
| 659 | goto bad; |
| 660 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 661 | /* nonconforming code segment */ |
| 662 | if (cpl != desc.dpl) |
| 663 | goto bad; |
| 664 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 665 | /* conforming code segment */ |
| 666 | if (cpl < desc.dpl) |
| 667 | goto bad; |
| 668 | } |
| 669 | break; |
| 670 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 671 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 672 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 673 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 674 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 675 | *linear = la; |
| 676 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 677 | bad: |
| 678 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 679 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 680 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 681 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 682 | } |
| 683 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 684 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 685 | struct segmented_address addr, |
| 686 | unsigned size, bool write, |
| 687 | ulong *linear) |
| 688 | { |
| 689 | return __linearize(ctxt, addr, size, write, false, linear); |
| 690 | } |
| 691 | |
| 692 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 693 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 694 | struct segmented_address addr, |
| 695 | void *data, |
| 696 | unsigned size) |
| 697 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 698 | int rc; |
| 699 | ulong linear; |
| 700 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 701 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 702 | if (rc != X86EMUL_CONTINUE) |
| 703 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 704 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 707 | /* |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 708 | * Prefetch the remaining bytes of the instruction without crossing page |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 709 | * boundary if they are not in fetch_cache yet. |
| 710 | */ |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 711 | static int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 712 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 713 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 714 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 715 | int size, cur_size; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 716 | unsigned long linear; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 717 | |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 718 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 719 | .ea = fc->end }; |
| 720 | cur_size = fc->end - fc->start; |
| 721 | size = min(15UL - cur_size, |
| 722 | PAGE_SIZE - offset_in_page(fc->end)); |
| 723 | if (unlikely(size == 0)) |
| 724 | return X86EMUL_UNHANDLEABLE; |
| 725 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
| 726 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 727 | return rc; |
| 728 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 729 | size, &ctxt->exception); |
| 730 | if (unlikely(rc != X86EMUL_CONTINUE)) |
| 731 | return rc; |
| 732 | fc->end += size; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 733 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 737 | void *__dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 738 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 739 | int rc; |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 740 | struct fetch_cache *fc = &ctxt->fetch; |
| 741 | u8 *dest = __dest; |
| 742 | u8 *src = &fc->data[ctxt->_eip - fc->start]; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 743 | |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 744 | while (size--) { |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 745 | if (unlikely(ctxt->_eip == fc->end)) { |
| 746 | rc = do_insn_fetch_bytes(ctxt); |
| 747 | if (rc != X86EMUL_CONTINUE) |
| 748 | return rc; |
| 749 | } |
| 750 | *dest++ = *src++; |
| 751 | ctxt->_eip++; |
| 752 | continue; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 753 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 754 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 757 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 758 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 759 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 760 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 761 | if (rc != X86EMUL_CONTINUE) \ |
| 762 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 763 | (_type)_x; \ |
| 764 | }) |
| 765 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 766 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 767 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 768 | if (rc != X86EMUL_CONTINUE) \ |
| 769 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 770 | }) |
| 771 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 772 | /* |
| 773 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 774 | * pointer into the block that addresses the relevant register. |
| 775 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 776 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 777 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 778 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 779 | { |
| 780 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 781 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 782 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 783 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 784 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 785 | else |
| 786 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 787 | return p; |
| 788 | } |
| 789 | |
| 790 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 791 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 792 | u16 *size, unsigned long *address, int op_bytes) |
| 793 | { |
| 794 | int rc; |
| 795 | |
| 796 | if (op_bytes == 2) |
| 797 | op_bytes = 3; |
| 798 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 799 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 800 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 801 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 802 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 803 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 804 | return rc; |
| 805 | } |
| 806 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 807 | FASTOP2(add); |
| 808 | FASTOP2(or); |
| 809 | FASTOP2(adc); |
| 810 | FASTOP2(sbb); |
| 811 | FASTOP2(and); |
| 812 | FASTOP2(sub); |
| 813 | FASTOP2(xor); |
| 814 | FASTOP2(cmp); |
| 815 | FASTOP2(test); |
| 816 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 817 | FASTOP1SRC2(mul, mul_ex); |
| 818 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 819 | FASTOP1SRC2EX(div, div_ex); |
| 820 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 821 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 822 | FASTOP3WCL(shld); |
| 823 | FASTOP3WCL(shrd); |
| 824 | |
| 825 | FASTOP2W(imul); |
| 826 | |
| 827 | FASTOP1(not); |
| 828 | FASTOP1(neg); |
| 829 | FASTOP1(inc); |
| 830 | FASTOP1(dec); |
| 831 | |
| 832 | FASTOP2CL(rol); |
| 833 | FASTOP2CL(ror); |
| 834 | FASTOP2CL(rcl); |
| 835 | FASTOP2CL(rcr); |
| 836 | FASTOP2CL(shl); |
| 837 | FASTOP2CL(shr); |
| 838 | FASTOP2CL(sar); |
| 839 | |
| 840 | FASTOP2W(bsf); |
| 841 | FASTOP2W(bsr); |
| 842 | FASTOP2W(bt); |
| 843 | FASTOP2W(bts); |
| 844 | FASTOP2W(btr); |
| 845 | FASTOP2W(btc); |
| 846 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 847 | FASTOP2(xadd); |
| 848 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 849 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 850 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 851 | u8 rc; |
| 852 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 853 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 854 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 855 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 856 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 857 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 858 | } |
| 859 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 860 | static void fetch_register_operand(struct operand *op) |
| 861 | { |
| 862 | switch (op->bytes) { |
| 863 | case 1: |
| 864 | op->val = *(u8 *)op->addr.reg; |
| 865 | break; |
| 866 | case 2: |
| 867 | op->val = *(u16 *)op->addr.reg; |
| 868 | break; |
| 869 | case 4: |
| 870 | op->val = *(u32 *)op->addr.reg; |
| 871 | break; |
| 872 | case 8: |
| 873 | op->val = *(u64 *)op->addr.reg; |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 878 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 879 | { |
| 880 | ctxt->ops->get_fpu(ctxt); |
| 881 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 882 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 883 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 884 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 885 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 886 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 887 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 888 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 889 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 890 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 891 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 892 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 893 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 894 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 895 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 896 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 897 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 898 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 899 | #endif |
| 900 | default: BUG(); |
| 901 | } |
| 902 | ctxt->ops->put_fpu(ctxt); |
| 903 | } |
| 904 | |
| 905 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 906 | int reg) |
| 907 | { |
| 908 | ctxt->ops->get_fpu(ctxt); |
| 909 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 910 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 911 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 912 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 913 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 914 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 915 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 916 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 917 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 918 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 919 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 920 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 921 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 922 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 923 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 924 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 925 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 926 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 927 | #endif |
| 928 | default: BUG(); |
| 929 | } |
| 930 | ctxt->ops->put_fpu(ctxt); |
| 931 | } |
| 932 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 933 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 934 | { |
| 935 | ctxt->ops->get_fpu(ctxt); |
| 936 | switch (reg) { |
| 937 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 938 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 939 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 940 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 941 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 942 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 943 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 944 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 945 | default: BUG(); |
| 946 | } |
| 947 | ctxt->ops->put_fpu(ctxt); |
| 948 | } |
| 949 | |
| 950 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 951 | { |
| 952 | ctxt->ops->get_fpu(ctxt); |
| 953 | switch (reg) { |
| 954 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 955 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 956 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 957 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 958 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 959 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 960 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 961 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 962 | default: BUG(); |
| 963 | } |
| 964 | ctxt->ops->put_fpu(ctxt); |
| 965 | } |
| 966 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 967 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 968 | { |
| 969 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 970 | return emulate_nm(ctxt); |
| 971 | |
| 972 | ctxt->ops->get_fpu(ctxt); |
| 973 | asm volatile("fninit"); |
| 974 | ctxt->ops->put_fpu(ctxt); |
| 975 | return X86EMUL_CONTINUE; |
| 976 | } |
| 977 | |
| 978 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 979 | { |
| 980 | u16 fcw; |
| 981 | |
| 982 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 983 | return emulate_nm(ctxt); |
| 984 | |
| 985 | ctxt->ops->get_fpu(ctxt); |
| 986 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 987 | ctxt->ops->put_fpu(ctxt); |
| 988 | |
| 989 | /* force 2 byte destination */ |
| 990 | ctxt->dst.bytes = 2; |
| 991 | ctxt->dst.val = fcw; |
| 992 | |
| 993 | return X86EMUL_CONTINUE; |
| 994 | } |
| 995 | |
| 996 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 997 | { |
| 998 | u16 fsw; |
| 999 | |
| 1000 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1001 | return emulate_nm(ctxt); |
| 1002 | |
| 1003 | ctxt->ops->get_fpu(ctxt); |
| 1004 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1005 | ctxt->ops->put_fpu(ctxt); |
| 1006 | |
| 1007 | /* force 2 byte destination */ |
| 1008 | ctxt->dst.bytes = 2; |
| 1009 | ctxt->dst.val = fsw; |
| 1010 | |
| 1011 | return X86EMUL_CONTINUE; |
| 1012 | } |
| 1013 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1014 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1015 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1016 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1017 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1018 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1019 | if (!(ctxt->d & ModRM)) |
| 1020 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1021 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1022 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1023 | op->type = OP_XMM; |
| 1024 | op->bytes = 16; |
| 1025 | op->addr.xmm = reg; |
| 1026 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1027 | return; |
| 1028 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1029 | if (ctxt->d & Mmx) { |
| 1030 | reg &= 7; |
| 1031 | op->type = OP_MM; |
| 1032 | op->bytes = 8; |
| 1033 | op->addr.mm = reg; |
| 1034 | return; |
| 1035 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1036 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1037 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1038 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1039 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1040 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1041 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1042 | op->orig_val = op->val; |
| 1043 | } |
| 1044 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1045 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1046 | { |
| 1047 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1048 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1049 | } |
| 1050 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1051 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1052 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1053 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1054 | u8 sib; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1055 | int index_reg, base_reg, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1056 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1057 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1058 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1059 | ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */ |
| 1060 | index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */ |
| 1061 | base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1062 | |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1063 | ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1064 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
Bandan Das | 02357bd | 2014-04-16 12:46:11 -0400 | [diff] [blame] | 1065 | ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1066 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1067 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1068 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1069 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1070 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1071 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1072 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1073 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1074 | op->type = OP_XMM; |
| 1075 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1076 | op->addr.xmm = ctxt->modrm_rm; |
| 1077 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1078 | return rc; |
| 1079 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1080 | if (ctxt->d & Mmx) { |
| 1081 | op->type = OP_MM; |
| 1082 | op->bytes = 8; |
Paolo Bonzini | bdc9072 | 2014-05-06 14:03:29 +0200 | [diff] [blame] | 1083 | op->addr.mm = ctxt->modrm_rm & 7; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1084 | return rc; |
| 1085 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1086 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1087 | return rc; |
| 1088 | } |
| 1089 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1090 | op->type = OP_MEM; |
| 1091 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1092 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1093 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1094 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1095 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1096 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1097 | |
| 1098 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1099 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1100 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1101 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1102 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1103 | break; |
| 1104 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1105 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1108 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1109 | break; |
| 1110 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1111 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1112 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1113 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1114 | break; |
| 1115 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1116 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1117 | break; |
| 1118 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1119 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1120 | break; |
| 1121 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1122 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1123 | break; |
| 1124 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1125 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1128 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1129 | break; |
| 1130 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1131 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1132 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1133 | break; |
| 1134 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1135 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1136 | break; |
| 1137 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1138 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1139 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1140 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1141 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1142 | } else { |
| 1143 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1144 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1145 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1146 | index_reg |= (sib >> 3) & 7; |
| 1147 | base_reg |= sib & 7; |
| 1148 | scale = sib >> 6; |
| 1149 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1150 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1151 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1152 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1153 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1154 | adjust_modrm_seg(ctxt, base_reg); |
| 1155 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1156 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1157 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1158 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1159 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1160 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1161 | } else { |
| 1162 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1163 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1164 | adjust_modrm_seg(ctxt, base_reg); |
| 1165 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1166 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1167 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1168 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1169 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1170 | break; |
| 1171 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1172 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1173 | break; |
| 1174 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1175 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1176 | break; |
| 1177 | } |
| 1178 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1179 | op->addr.mem.ea = modrm_ea; |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 1180 | if (ctxt->ad_bytes != 8) |
| 1181 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
| 1182 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1183 | done: |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
| 1187 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1188 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1189 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1190 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1191 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1192 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1193 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1194 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1195 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1196 | break; |
| 1197 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1198 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1199 | break; |
| 1200 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1201 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | done: |
| 1205 | return rc; |
| 1206 | } |
| 1207 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1208 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1209 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1210 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1211 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1212 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1213 | mask = ~((long)ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1214 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1215 | if (ctxt->src.bytes == 2) |
| 1216 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1217 | else if (ctxt->src.bytes == 4) |
| 1218 | sv = (s32)ctxt->src.val & (s32)mask; |
Nadav Amit | 7dec560 | 2014-06-15 16:12:57 +0300 | [diff] [blame] | 1219 | else |
| 1220 | sv = (s64)ctxt->src.val & (s64)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1221 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1222 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1223 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1224 | |
| 1225 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1226 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1229 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1230 | unsigned long addr, void *dest, unsigned size) |
| 1231 | { |
| 1232 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1233 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1234 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1235 | if (mc->pos < mc->end) |
| 1236 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1237 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1238 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1239 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1240 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1241 | &ctxt->exception); |
| 1242 | if (rc != X86EMUL_CONTINUE) |
| 1243 | return rc; |
| 1244 | |
| 1245 | mc->end += size; |
| 1246 | |
| 1247 | read_cached: |
| 1248 | memcpy(dest, mc->data + mc->pos, size); |
| 1249 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1250 | return X86EMUL_CONTINUE; |
| 1251 | } |
| 1252 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1253 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1254 | struct segmented_address addr, |
| 1255 | void *data, |
| 1256 | unsigned size) |
| 1257 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1258 | int rc; |
| 1259 | ulong linear; |
| 1260 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1261 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1262 | if (rc != X86EMUL_CONTINUE) |
| 1263 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1264 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1268 | struct segmented_address addr, |
| 1269 | const void *data, |
| 1270 | unsigned size) |
| 1271 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1272 | int rc; |
| 1273 | ulong linear; |
| 1274 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1275 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1276 | if (rc != X86EMUL_CONTINUE) |
| 1277 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1278 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1279 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1283 | struct segmented_address addr, |
| 1284 | const void *orig_data, const void *data, |
| 1285 | unsigned size) |
| 1286 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1287 | int rc; |
| 1288 | ulong linear; |
| 1289 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1290 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1291 | if (rc != X86EMUL_CONTINUE) |
| 1292 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1293 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1294 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1297 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1298 | unsigned int size, unsigned short port, |
| 1299 | void *dest) |
| 1300 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1301 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1302 | |
| 1303 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1304 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1305 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1306 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1307 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1308 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1309 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1310 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1311 | count); |
| 1312 | if (n == 0) |
| 1313 | n = 1; |
| 1314 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1315 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1316 | return 0; |
| 1317 | rc->end = n * size; |
| 1318 | } |
| 1319 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1320 | if (ctxt->rep_prefix && (ctxt->d & String) && |
| 1321 | !(ctxt->eflags & EFLG_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1322 | ctxt->dst.data = rc->data + rc->pos; |
| 1323 | ctxt->dst.type = OP_MEM_STR; |
| 1324 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1325 | rc->pos = rc->end; |
| 1326 | } else { |
| 1327 | memcpy(dest, rc->data + rc->pos, size); |
| 1328 | rc->pos += size; |
| 1329 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1330 | return 1; |
| 1331 | } |
| 1332 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1333 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1334 | u16 index, struct desc_struct *desc) |
| 1335 | { |
| 1336 | struct desc_ptr dt; |
| 1337 | ulong addr; |
| 1338 | |
| 1339 | ctxt->ops->get_idt(ctxt, &dt); |
| 1340 | |
| 1341 | if (dt.size < index * 8 + 7) |
| 1342 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1343 | |
| 1344 | addr = dt.address + index * 8; |
| 1345 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1346 | &ctxt->exception); |
| 1347 | } |
| 1348 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1349 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1350 | u16 selector, struct desc_ptr *dt) |
| 1351 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1352 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1353 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1354 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1355 | if (selector & 1 << 2) { |
| 1356 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1357 | u16 sel; |
| 1358 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1359 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1360 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1361 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1362 | return; |
| 1363 | |
| 1364 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1365 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1366 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1367 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /* allowed just for 8 bytes segments */ |
| 1371 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1372 | u16 selector, struct desc_struct *desc, |
| 1373 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1374 | { |
| 1375 | struct desc_ptr dt; |
| 1376 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1377 | ulong addr; |
| 1378 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1379 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1380 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1381 | if (dt.size < index * 8 + 7) |
| 1382 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1383 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1384 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1385 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1386 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /* allowed just for 8 bytes segments */ |
| 1390 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1391 | u16 selector, struct desc_struct *desc) |
| 1392 | { |
| 1393 | struct desc_ptr dt; |
| 1394 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1395 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1396 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1397 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1398 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1399 | if (dt.size < index * 8 + 7) |
| 1400 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1401 | |
| 1402 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1403 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1404 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1407 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1408 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1409 | u16 selector, int seg, u8 cpl, bool in_task_switch) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1410 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1411 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1412 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1413 | unsigned err_vec = GP_VECTOR; |
| 1414 | u32 err_code = 0; |
| 1415 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1416 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1417 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1418 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1419 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1420 | |
| 1421 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1422 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1423 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1424 | /* set real mode segment descriptor (keep limit etc. for |
| 1425 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1426 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1427 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1428 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1429 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1430 | /* VM86 needs a clean new segment descriptor */ |
| 1431 | set_desc_base(&seg_desc, selector << 4); |
| 1432 | set_desc_limit(&seg_desc, 0xffff); |
| 1433 | seg_desc.type = 3; |
| 1434 | seg_desc.p = 1; |
| 1435 | seg_desc.s = 1; |
| 1436 | seg_desc.dpl = 3; |
| 1437 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1440 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1441 | |
| 1442 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1443 | if ((seg == VCPU_SREG_CS |
| 1444 | || (seg == VCPU_SREG_SS |
| 1445 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1446 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1447 | && null_selector) |
| 1448 | goto exception; |
| 1449 | |
| 1450 | /* TR should be in GDT only */ |
| 1451 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1452 | goto exception; |
| 1453 | |
| 1454 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1455 | goto load; |
| 1456 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1457 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1458 | if (ret != X86EMUL_CONTINUE) |
| 1459 | return ret; |
| 1460 | |
| 1461 | err_code = selector & 0xfffc; |
| 1462 | err_vec = GP_VECTOR; |
| 1463 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1464 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1465 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1466 | goto exception; |
| 1467 | |
| 1468 | if (!seg_desc.p) { |
| 1469 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1470 | goto exception; |
| 1471 | } |
| 1472 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1473 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1474 | |
| 1475 | switch (seg) { |
| 1476 | case VCPU_SREG_SS: |
| 1477 | /* |
| 1478 | * segment is not a writable data segment or segment |
| 1479 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1480 | */ |
| 1481 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1482 | goto exception; |
| 1483 | break; |
| 1484 | case VCPU_SREG_CS: |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1485 | if (in_task_switch && rpl != dpl) |
| 1486 | goto exception; |
| 1487 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1488 | if (!(seg_desc.type & 8)) |
| 1489 | goto exception; |
| 1490 | |
| 1491 | if (seg_desc.type & 4) { |
| 1492 | /* conforming */ |
| 1493 | if (dpl > cpl) |
| 1494 | goto exception; |
| 1495 | } else { |
| 1496 | /* nonconforming */ |
| 1497 | if (rpl > cpl || dpl != cpl) |
| 1498 | goto exception; |
| 1499 | } |
| 1500 | /* CS(RPL) <- CPL */ |
| 1501 | selector = (selector & 0xfffc) | cpl; |
| 1502 | break; |
| 1503 | case VCPU_SREG_TR: |
| 1504 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1505 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1506 | old_desc = seg_desc; |
| 1507 | seg_desc.type |= 2; /* busy */ |
| 1508 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1509 | sizeof(seg_desc), &ctxt->exception); |
| 1510 | if (ret != X86EMUL_CONTINUE) |
| 1511 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1512 | break; |
| 1513 | case VCPU_SREG_LDTR: |
| 1514 | if (seg_desc.s || seg_desc.type != 2) |
| 1515 | goto exception; |
| 1516 | break; |
| 1517 | default: /* DS, ES, FS, or GS */ |
| 1518 | /* |
| 1519 | * segment is not a data or readable code segment or |
| 1520 | * ((segment is a data or nonconforming code segment) |
| 1521 | * and (both RPL and CPL > DPL)) |
| 1522 | */ |
| 1523 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1524 | (((seg_desc.type & 0xc) != 0xc) && |
| 1525 | (rpl > dpl && cpl > dpl))) |
| 1526 | goto exception; |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | if (seg_desc.s) { |
| 1531 | /* mark segment as accessed */ |
| 1532 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1533 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1534 | if (ret != X86EMUL_CONTINUE) |
| 1535 | return ret; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1536 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1537 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1538 | sizeof(base3), &ctxt->exception); |
| 1539 | if (ret != X86EMUL_CONTINUE) |
| 1540 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1541 | } |
| 1542 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1543 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1544 | return X86EMUL_CONTINUE; |
| 1545 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1546 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1547 | return X86EMUL_PROPAGATE_FAULT; |
| 1548 | } |
| 1549 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1550 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1551 | u16 selector, int seg) |
| 1552 | { |
| 1553 | u8 cpl = ctxt->ops->cpl(ctxt); |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1554 | return __load_segment_descriptor(ctxt, selector, seg, cpl, false); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1557 | static void write_register_operand(struct operand *op) |
| 1558 | { |
| 1559 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1560 | switch (op->bytes) { |
| 1561 | case 1: |
| 1562 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1563 | break; |
| 1564 | case 2: |
| 1565 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1566 | break; |
| 1567 | case 4: |
| 1568 | *op->addr.reg = (u32)op->val; |
| 1569 | break; /* 64b: zero-extend */ |
| 1570 | case 8: |
| 1571 | *op->addr.reg = op->val; |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1576 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1577 | { |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1578 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1579 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1580 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1581 | break; |
| 1582 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1583 | if (ctxt->lock_prefix) |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1584 | return segmented_cmpxchg(ctxt, |
| 1585 | op->addr.mem, |
| 1586 | &op->orig_val, |
| 1587 | &op->val, |
| 1588 | op->bytes); |
| 1589 | else |
| 1590 | return segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1591 | op->addr.mem, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1592 | &op->val, |
| 1593 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1594 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1595 | case OP_MEM_STR: |
Paolo Bonzini | f5f87df | 2014-04-01 13:23:24 +0200 | [diff] [blame] | 1596 | return segmented_write(ctxt, |
| 1597 | op->addr.mem, |
| 1598 | op->data, |
| 1599 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1600 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1601 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1602 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1603 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1604 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1605 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1606 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1607 | case OP_NONE: |
| 1608 | /* no writeback */ |
| 1609 | break; |
| 1610 | default: |
| 1611 | break; |
| 1612 | } |
| 1613 | return X86EMUL_CONTINUE; |
| 1614 | } |
| 1615 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1616 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1617 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1618 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1619 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1620 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1621 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1622 | addr.seg = VCPU_SREG_SS; |
| 1623 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1624 | return segmented_write(ctxt, addr, data, bytes); |
| 1625 | } |
| 1626 | |
| 1627 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1628 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1629 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1630 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1631 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1634 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1635 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1636 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1637 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1638 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1639 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1640 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1641 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1642 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1643 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1644 | return rc; |
| 1645 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1646 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1647 | return rc; |
| 1648 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1649 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1650 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1651 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1652 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1653 | } |
| 1654 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1655 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1656 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1657 | { |
| 1658 | int rc; |
| 1659 | unsigned long val, change_mask; |
| 1660 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1661 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1662 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1663 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1664 | if (rc != X86EMUL_CONTINUE) |
| 1665 | return rc; |
| 1666 | |
| 1667 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1668 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1669 | |
| 1670 | switch(ctxt->mode) { |
| 1671 | case X86EMUL_MODE_PROT64: |
| 1672 | case X86EMUL_MODE_PROT32: |
| 1673 | case X86EMUL_MODE_PROT16: |
| 1674 | if (cpl == 0) |
| 1675 | change_mask |= EFLG_IOPL; |
| 1676 | if (cpl <= iopl) |
| 1677 | change_mask |= EFLG_IF; |
| 1678 | break; |
| 1679 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1680 | if (iopl < 3) |
| 1681 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1682 | change_mask |= EFLG_IF; |
| 1683 | break; |
| 1684 | default: /* real mode */ |
| 1685 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1686 | break; |
| 1687 | } |
| 1688 | |
| 1689 | *(unsigned long *)dest = |
| 1690 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1691 | |
| 1692 | return rc; |
| 1693 | } |
| 1694 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1695 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1696 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1697 | ctxt->dst.type = OP_REG; |
| 1698 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1699 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1700 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1701 | } |
| 1702 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1703 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1704 | { |
| 1705 | int rc; |
| 1706 | unsigned frame_size = ctxt->src.val; |
| 1707 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1708 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1709 | |
| 1710 | if (nesting_level) |
| 1711 | return X86EMUL_UNHANDLEABLE; |
| 1712 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1713 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1714 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1715 | if (rc != X86EMUL_CONTINUE) |
| 1716 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1717 | 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] | 1718 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1719 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1720 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1721 | stack_mask(ctxt)); |
| 1722 | return X86EMUL_CONTINUE; |
| 1723 | } |
| 1724 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1725 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1726 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1727 | 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] | 1728 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1729 | 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] | 1730 | } |
| 1731 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1732 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1733 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1734 | int seg = ctxt->src2.val; |
| 1735 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1736 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1737 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1738 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1739 | } |
| 1740 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1741 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1742 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1743 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1744 | unsigned long selector; |
| 1745 | int rc; |
| 1746 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1747 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1748 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1749 | return rc; |
| 1750 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame] | 1751 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1752 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 1753 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1754 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1755 | return rc; |
| 1756 | } |
| 1757 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1758 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1759 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1760 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1761 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1762 | int reg = VCPU_REGS_RAX; |
| 1763 | |
| 1764 | while (reg <= VCPU_REGS_RDI) { |
| 1765 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1766 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1767 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1768 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1769 | if (rc != X86EMUL_CONTINUE) |
| 1770 | return rc; |
| 1771 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1772 | ++reg; |
| 1773 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1774 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1775 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1778 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1779 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1780 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1781 | return em_push(ctxt); |
| 1782 | } |
| 1783 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1784 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1785 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1786 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1787 | int reg = VCPU_REGS_RDI; |
| 1788 | |
| 1789 | while (reg >= VCPU_REGS_RAX) { |
| 1790 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1791 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1792 | --reg; |
| 1793 | } |
| 1794 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1795 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1796 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1797 | break; |
| 1798 | --reg; |
| 1799 | } |
| 1800 | return rc; |
| 1801 | } |
| 1802 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1803 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1804 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1805 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1806 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1807 | struct desc_ptr dt; |
| 1808 | gva_t cs_addr; |
| 1809 | gva_t eip_addr; |
| 1810 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1811 | |
| 1812 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1813 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1814 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1815 | if (rc != X86EMUL_CONTINUE) |
| 1816 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1817 | |
| 1818 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1819 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1820 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1821 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1822 | if (rc != X86EMUL_CONTINUE) |
| 1823 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1824 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1825 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1826 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1827 | if (rc != X86EMUL_CONTINUE) |
| 1828 | return rc; |
| 1829 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1830 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1831 | |
| 1832 | eip_addr = dt.address + (irq << 2); |
| 1833 | cs_addr = dt.address + (irq << 2) + 2; |
| 1834 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1835 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1836 | if (rc != X86EMUL_CONTINUE) |
| 1837 | return rc; |
| 1838 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1839 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1840 | if (rc != X86EMUL_CONTINUE) |
| 1841 | return rc; |
| 1842 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1843 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1844 | if (rc != X86EMUL_CONTINUE) |
| 1845 | return rc; |
| 1846 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1847 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1848 | |
| 1849 | return rc; |
| 1850 | } |
| 1851 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1852 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1853 | { |
| 1854 | int rc; |
| 1855 | |
| 1856 | invalidate_registers(ctxt); |
| 1857 | rc = __emulate_int_real(ctxt, irq); |
| 1858 | if (rc == X86EMUL_CONTINUE) |
| 1859 | writeback_registers(ctxt); |
| 1860 | return rc; |
| 1861 | } |
| 1862 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1863 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1864 | { |
| 1865 | switch(ctxt->mode) { |
| 1866 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1867 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1868 | case X86EMUL_MODE_VM86: |
| 1869 | case X86EMUL_MODE_PROT16: |
| 1870 | case X86EMUL_MODE_PROT32: |
| 1871 | case X86EMUL_MODE_PROT64: |
| 1872 | default: |
| 1873 | /* Protected mode interrupts unimplemented yet */ |
| 1874 | return X86EMUL_UNHANDLEABLE; |
| 1875 | } |
| 1876 | } |
| 1877 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1878 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1879 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1880 | int rc = X86EMUL_CONTINUE; |
| 1881 | unsigned long temp_eip = 0; |
| 1882 | unsigned long temp_eflags = 0; |
| 1883 | unsigned long cs = 0; |
| 1884 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1885 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1886 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1887 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1888 | |
| 1889 | /* TODO: Add stack limit check */ |
| 1890 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1891 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1892 | |
| 1893 | if (rc != X86EMUL_CONTINUE) |
| 1894 | return rc; |
| 1895 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1896 | if (temp_eip & ~0xffff) |
| 1897 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1898 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1899 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1900 | |
| 1901 | if (rc != X86EMUL_CONTINUE) |
| 1902 | return rc; |
| 1903 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1904 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1905 | |
| 1906 | if (rc != X86EMUL_CONTINUE) |
| 1907 | return rc; |
| 1908 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1909 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1910 | |
| 1911 | if (rc != X86EMUL_CONTINUE) |
| 1912 | return rc; |
| 1913 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1914 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1915 | |
| 1916 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1917 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1918 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1919 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1920 | ctxt->eflags &= ~0xffff; |
| 1921 | ctxt->eflags |= temp_eflags; |
| 1922 | } |
| 1923 | |
| 1924 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1925 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1926 | |
| 1927 | return rc; |
| 1928 | } |
| 1929 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 1930 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1931 | { |
| 1932 | switch(ctxt->mode) { |
| 1933 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1934 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1935 | case X86EMUL_MODE_VM86: |
| 1936 | case X86EMUL_MODE_PROT16: |
| 1937 | case X86EMUL_MODE_PROT32: |
| 1938 | case X86EMUL_MODE_PROT64: |
| 1939 | default: |
| 1940 | /* iret from protected mode unimplemented yet */ |
| 1941 | return X86EMUL_UNHANDLEABLE; |
| 1942 | } |
| 1943 | } |
| 1944 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1945 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 1946 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1947 | int rc; |
| 1948 | unsigned short sel; |
| 1949 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1950 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1951 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1952 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1953 | if (rc != X86EMUL_CONTINUE) |
| 1954 | return rc; |
| 1955 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1956 | ctxt->_eip = 0; |
| 1957 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1958 | return X86EMUL_CONTINUE; |
| 1959 | } |
| 1960 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1961 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1962 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1963 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1964 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1965 | switch (ctxt->modrm_reg) { |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1966 | case 2: /* call near abs */ { |
| 1967 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1968 | old_eip = ctxt->_eip; |
| 1969 | ctxt->_eip = ctxt->src.val; |
| 1970 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1971 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1972 | break; |
| 1973 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1974 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1975 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1976 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1977 | case 5: /* jmp far */ |
| 1978 | rc = em_jmp_far(ctxt); |
| 1979 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1980 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1981 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1982 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1983 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1984 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 1987 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1988 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1989 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1990 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 1991 | if (ctxt->dst.bytes == 16) |
| 1992 | return X86EMUL_UNHANDLEABLE; |
| 1993 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1994 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 1995 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 1996 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 1997 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1998 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1999 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2000 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2001 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2002 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2003 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2004 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2005 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2008 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2009 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2010 | ctxt->dst.type = OP_REG; |
| 2011 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2012 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2013 | return em_pop(ctxt); |
| 2014 | } |
| 2015 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2016 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2017 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2018 | int rc; |
| 2019 | unsigned long cs; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2020 | int cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2021 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2022 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2023 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2024 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2025 | if (ctxt->op_bytes == 4) |
| 2026 | ctxt->_eip = (u32)ctxt->_eip; |
| 2027 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2028 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2029 | return rc; |
Nadav Amit | 9e8919a | 2014-06-15 16:12:59 +0300 | [diff] [blame] | 2030 | /* Outer-privilege level return is not implemented */ |
| 2031 | if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl) |
| 2032 | return X86EMUL_UNHANDLEABLE; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2033 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2034 | return rc; |
| 2035 | } |
| 2036 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2037 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2038 | { |
| 2039 | int rc; |
| 2040 | |
| 2041 | rc = em_ret_far(ctxt); |
| 2042 | if (rc != X86EMUL_CONTINUE) |
| 2043 | return rc; |
| 2044 | rsp_increment(ctxt, ctxt->src.val); |
| 2045 | return X86EMUL_CONTINUE; |
| 2046 | } |
| 2047 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2048 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2049 | { |
| 2050 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2051 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2052 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2053 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2054 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2055 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2056 | |
| 2057 | if (ctxt->eflags & EFLG_ZF) { |
| 2058 | /* Success: write back to memory. */ |
| 2059 | ctxt->dst.val = ctxt->src.orig_val; |
| 2060 | } else { |
| 2061 | /* Failure: write the value we saw to EAX. */ |
| 2062 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2063 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2064 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2065 | } |
| 2066 | return X86EMUL_CONTINUE; |
| 2067 | } |
| 2068 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2069 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2070 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2071 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2072 | unsigned short sel; |
| 2073 | int rc; |
| 2074 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2075 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2076 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2077 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2078 | if (rc != X86EMUL_CONTINUE) |
| 2079 | return rc; |
| 2080 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2081 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2082 | return rc; |
| 2083 | } |
| 2084 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2085 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2086 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2087 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2088 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2089 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2090 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2091 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2092 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2093 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2094 | cs->s = 1; |
| 2095 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2096 | cs->p = 1; |
| 2097 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2098 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2099 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2100 | set_desc_base(ss, 0); /* flat segment */ |
| 2101 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2102 | ss->g = 1; /* 4kb granularity */ |
| 2103 | ss->s = 1; |
| 2104 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2105 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2106 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2107 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2108 | ss->l = 0; |
| 2109 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2112 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2113 | { |
| 2114 | u32 eax, ebx, ecx, edx; |
| 2115 | |
| 2116 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2117 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2118 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2119 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2120 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2121 | } |
| 2122 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2123 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2124 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2125 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2126 | u32 eax, ebx, ecx, edx; |
| 2127 | |
| 2128 | /* |
| 2129 | * syscall should always be enabled in longmode - so only become |
| 2130 | * vendor specific (cpuid) if other modes are active... |
| 2131 | */ |
| 2132 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2133 | return true; |
| 2134 | |
| 2135 | eax = 0x00000000; |
| 2136 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2137 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2138 | /* |
| 2139 | * Intel ("GenuineIntel") |
| 2140 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2141 | * longmode. Also an 64bit guest with a |
| 2142 | * 32bit compat-app running will #UD !! While this |
| 2143 | * behaviour can be fixed (by emulating) into AMD |
| 2144 | * response - CPUs of AMD can't behave like Intel. |
| 2145 | */ |
| 2146 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2147 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2148 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2149 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2150 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2151 | /* AMD ("AuthenticAMD") */ |
| 2152 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2153 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2154 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2155 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2156 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2157 | /* AMD ("AMDisbetter!") */ |
| 2158 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2159 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2160 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2161 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2162 | |
| 2163 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2167 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2168 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2169 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2170 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2171 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2172 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2173 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2174 | |
| 2175 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2176 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2177 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2178 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2179 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2180 | if (!(em_syscall_is_enabled(ctxt))) |
| 2181 | return emulate_ud(ctxt); |
| 2182 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2183 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2184 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2185 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2186 | if (!(efer & EFER_SCE)) |
| 2187 | return emulate_ud(ctxt); |
| 2188 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2189 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2190 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2191 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2192 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2193 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2194 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2195 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2196 | cs.l = 1; |
| 2197 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2198 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2199 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2200 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2201 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2202 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2203 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2204 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2205 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2206 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2207 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2208 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2209 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2210 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2211 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2212 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2213 | #endif |
| 2214 | } else { |
| 2215 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2216 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2217 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2218 | |
| 2219 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2220 | } |
| 2221 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2222 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2223 | } |
| 2224 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2225 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2226 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2227 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2228 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2229 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2230 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2231 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2232 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2233 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2234 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2235 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2236 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2237 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2238 | /* |
| 2239 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2240 | * mode). |
| 2241 | */ |
| 2242 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2243 | && !vendor_intel(ctxt)) |
| 2244 | return emulate_ud(ctxt); |
| 2245 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2246 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2247 | * Therefore, we inject an #UD. |
| 2248 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2249 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2250 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2251 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2252 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2253 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2254 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2255 | switch (ctxt->mode) { |
| 2256 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2257 | if ((msr_data & 0xfffc) == 0x0) |
| 2258 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2259 | break; |
| 2260 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2261 | if (msr_data == 0x0) |
| 2262 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2263 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2264 | default: |
| 2265 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2269 | cs_sel = (u16)msr_data; |
| 2270 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2271 | ss_sel = cs_sel + 8; |
| 2272 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2273 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2274 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2275 | cs.l = 1; |
| 2276 | } |
| 2277 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2278 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2279 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2280 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2281 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2282 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2283 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2284 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2285 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2286 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2287 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2290 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2291 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2292 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2293 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2294 | u64 msr_data; |
| 2295 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2296 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2297 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2298 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2299 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2300 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2301 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2302 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2303 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2304 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2305 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2306 | usermode = X86EMUL_MODE_PROT64; |
| 2307 | else |
| 2308 | usermode = X86EMUL_MODE_PROT32; |
| 2309 | |
| 2310 | cs.dpl = 3; |
| 2311 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2312 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2313 | switch (usermode) { |
| 2314 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2315 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2316 | if ((msr_data & 0xfffc) == 0x0) |
| 2317 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2318 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2319 | break; |
| 2320 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2321 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2322 | if (msr_data == 0x0) |
| 2323 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2324 | ss_sel = cs_sel + 8; |
| 2325 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2326 | cs.l = 1; |
| 2327 | break; |
| 2328 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2329 | cs_sel |= SELECTOR_RPL_MASK; |
| 2330 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2331 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2332 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2333 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2334 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2335 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2336 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2337 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2338 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2341 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2342 | { |
| 2343 | int iopl; |
| 2344 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2345 | return false; |
| 2346 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2347 | return true; |
| 2348 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2349 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2353 | u16 port, u16 len) |
| 2354 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2355 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2356 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2357 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2358 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2359 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2360 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2361 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2362 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2363 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2364 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2365 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2366 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2367 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2368 | base = get_desc_base(&tr_seg); |
| 2369 | #ifdef CONFIG_X86_64 |
| 2370 | base |= ((u64)base3) << 32; |
| 2371 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2372 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2373 | if (r != X86EMUL_CONTINUE) |
| 2374 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2375 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2376 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2377 | 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] | 2378 | if (r != X86EMUL_CONTINUE) |
| 2379 | return false; |
| 2380 | if ((perm >> bit_idx) & mask) |
| 2381 | return false; |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2386 | u16 port, u16 len) |
| 2387 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2388 | if (ctxt->perm_ok) |
| 2389 | return true; |
| 2390 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2391 | if (emulator_bad_iopl(ctxt)) |
| 2392 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2393 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2394 | |
| 2395 | ctxt->perm_ok = true; |
| 2396 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2397 | return true; |
| 2398 | } |
| 2399 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2400 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2401 | struct tss_segment_16 *tss) |
| 2402 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2403 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2404 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2405 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2406 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2407 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2408 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2409 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2410 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2411 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2412 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2413 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2414 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2415 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2416 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2417 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2418 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2422 | struct tss_segment_16 *tss) |
| 2423 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2424 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2425 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2426 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2427 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2428 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2429 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2430 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2431 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2432 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2433 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2434 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2435 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2436 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2437 | |
| 2438 | /* |
| 2439 | * SDM says that segment selectors are loaded before segment |
| 2440 | * descriptors |
| 2441 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2442 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2443 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2444 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2445 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2446 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2447 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2448 | cpl = tss->cs & 3; |
| 2449 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2450 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2451 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2452 | * it is handled in a context of new task |
| 2453 | */ |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2454 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2455 | if (ret != X86EMUL_CONTINUE) |
| 2456 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2457 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2458 | if (ret != X86EMUL_CONTINUE) |
| 2459 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2460 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2461 | if (ret != X86EMUL_CONTINUE) |
| 2462 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2463 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2464 | if (ret != X86EMUL_CONTINUE) |
| 2465 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2466 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2467 | if (ret != X86EMUL_CONTINUE) |
| 2468 | return ret; |
| 2469 | |
| 2470 | return X86EMUL_CONTINUE; |
| 2471 | } |
| 2472 | |
| 2473 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2474 | u16 tss_selector, u16 old_tss_sel, |
| 2475 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2476 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2477 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2478 | struct tss_segment_16 tss_seg; |
| 2479 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2480 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2481 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2482 | 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] | 2483 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2484 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2485 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2486 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2487 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2488 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2489 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2490 | 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] | 2491 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2492 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2493 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2494 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2495 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2496 | 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] | 2497 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2498 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2499 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2500 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2501 | |
| 2502 | if (old_tss_sel != 0xffff) { |
| 2503 | tss_seg.prev_task_link = old_tss_sel; |
| 2504 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2505 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2506 | &tss_seg.prev_task_link, |
| 2507 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2508 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2509 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2510 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2511 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2514 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2518 | struct tss_segment_32 *tss) |
| 2519 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2520 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2521 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2522 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2523 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2524 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2525 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2526 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2527 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2528 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2529 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2530 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2531 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2532 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2533 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2534 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2535 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2536 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2537 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2541 | struct tss_segment_32 *tss) |
| 2542 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2543 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2544 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2545 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2546 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2547 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2548 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2549 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2550 | |
| 2551 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2552 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2553 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2554 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2555 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2556 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2557 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2558 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2559 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2560 | |
| 2561 | /* |
| 2562 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2563 | * descriptors. This is important because CPL checks will |
| 2564 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2565 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2566 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2567 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2568 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2569 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2570 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2571 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2572 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2573 | |
| 2574 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2575 | * If we're switching between Protected Mode and VM86, we need to make |
| 2576 | * sure to update the mode before loading the segment descriptors so |
| 2577 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2578 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2579 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2580 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2581 | cpl = 3; |
| 2582 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2583 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2584 | cpl = tss->cs & 3; |
| 2585 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2586 | |
| 2587 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2588 | * Now load segment descriptors. If fault happenes at this stage |
| 2589 | * it is handled in a context of new task |
| 2590 | */ |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2591 | ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2592 | if (ret != X86EMUL_CONTINUE) |
| 2593 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2594 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2595 | if (ret != X86EMUL_CONTINUE) |
| 2596 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2597 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | if (ret != X86EMUL_CONTINUE) |
| 2599 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2600 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2601 | if (ret != X86EMUL_CONTINUE) |
| 2602 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2603 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2604 | if (ret != X86EMUL_CONTINUE) |
| 2605 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2606 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2607 | if (ret != X86EMUL_CONTINUE) |
| 2608 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2609 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2610 | if (ret != X86EMUL_CONTINUE) |
| 2611 | return ret; |
| 2612 | |
| 2613 | return X86EMUL_CONTINUE; |
| 2614 | } |
| 2615 | |
| 2616 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2617 | u16 tss_selector, u16 old_tss_sel, |
| 2618 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2619 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2620 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2621 | struct tss_segment_32 tss_seg; |
| 2622 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2623 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2624 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2625 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2626 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2627 | ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2628 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2629 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2630 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2632 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2633 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2634 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2635 | /* Only GP registers and segment selectors are saved */ |
| 2636 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2637 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2638 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2639 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2641 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2642 | 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] | 2643 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2644 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2645 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2646 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | |
| 2648 | if (old_tss_sel != 0xffff) { |
| 2649 | tss_seg.prev_task_link = old_tss_sel; |
| 2650 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2651 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2652 | &tss_seg.prev_task_link, |
| 2653 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2654 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2655 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2656 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2657 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2658 | } |
| 2659 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2660 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2664 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2665 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2667 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2668 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2669 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2670 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2671 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2672 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2673 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2674 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2675 | |
| 2676 | /* FIXME: old_tss_base == ~0 ? */ |
| 2677 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2678 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2679 | if (ret != X86EMUL_CONTINUE) |
| 2680 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2681 | 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] | 2682 | if (ret != X86EMUL_CONTINUE) |
| 2683 | return ret; |
| 2684 | |
| 2685 | /* FIXME: check that next_tss_desc is tss */ |
| 2686 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2687 | /* |
| 2688 | * Check privileges. The three cases are task switch caused by... |
| 2689 | * |
| 2690 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2691 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2692 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2693 | */ |
| 2694 | if (reason == TASK_SWITCH_GATE) { |
| 2695 | if (idt_index != -1) { |
| 2696 | /* Software interrupts */ |
| 2697 | struct desc_struct task_gate_desc; |
| 2698 | int dpl; |
| 2699 | |
| 2700 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2701 | &task_gate_desc); |
| 2702 | if (ret != X86EMUL_CONTINUE) |
| 2703 | return ret; |
| 2704 | |
| 2705 | dpl = task_gate_desc.dpl; |
| 2706 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2707 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2708 | } |
| 2709 | } else if (reason != TASK_SWITCH_IRET) { |
| 2710 | int dpl = next_tss_desc.dpl; |
| 2711 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2712 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2715 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2716 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2717 | if (!next_tss_desc.p || |
| 2718 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2719 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2720 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2721 | return X86EMUL_PROPAGATE_FAULT; |
| 2722 | } |
| 2723 | |
| 2724 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2725 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2726 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | if (reason == TASK_SWITCH_IRET) |
| 2730 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2731 | |
| 2732 | /* 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] | 2733 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2734 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2735 | old_tss_sel = 0xffff; |
| 2736 | |
| 2737 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2738 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2739 | old_tss_base, &next_tss_desc); |
| 2740 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2741 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2742 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2743 | if (ret != X86EMUL_CONTINUE) |
| 2744 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2745 | |
| 2746 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2747 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2748 | |
| 2749 | if (reason != TASK_SWITCH_IRET) { |
| 2750 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2751 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2752 | } |
| 2753 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2754 | 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] | 2755 | 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] | 2756 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2757 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2758 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2759 | ctxt->lock_prefix = 0; |
| 2760 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2761 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2764 | return ret; |
| 2765 | } |
| 2766 | |
| 2767 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2768 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2769 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2770 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2771 | int rc; |
| 2772 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2773 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2774 | ctxt->_eip = ctxt->eip; |
| 2775 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2776 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2777 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2778 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2779 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2780 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2781 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2782 | writeback_registers(ctxt); |
| 2783 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2784 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2785 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2788 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2789 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2790 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2791 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2792 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2793 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2794 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2797 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2798 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2799 | u8 al, old_al; |
| 2800 | bool af, cf, old_cf; |
| 2801 | |
| 2802 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2803 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2804 | |
| 2805 | old_al = al; |
| 2806 | old_cf = cf; |
| 2807 | cf = false; |
| 2808 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2809 | if ((al & 0x0f) > 9 || af) { |
| 2810 | al -= 6; |
| 2811 | cf = old_cf | (al >= 250); |
| 2812 | af = true; |
| 2813 | } else { |
| 2814 | af = false; |
| 2815 | } |
| 2816 | if (old_al > 0x99 || old_cf) { |
| 2817 | al -= 0x60; |
| 2818 | cf = true; |
| 2819 | } |
| 2820 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2821 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2822 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2823 | ctxt->src.type = OP_IMM; |
| 2824 | ctxt->src.val = 0; |
| 2825 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2826 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2827 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2828 | if (cf) |
| 2829 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2830 | if (af) |
| 2831 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2832 | return X86EMUL_CONTINUE; |
| 2833 | } |
| 2834 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2835 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2836 | { |
| 2837 | u8 al, ah; |
| 2838 | |
| 2839 | if (ctxt->src.val == 0) |
| 2840 | return emulate_de(ctxt); |
| 2841 | |
| 2842 | al = ctxt->dst.val & 0xff; |
| 2843 | ah = al / ctxt->src.val; |
| 2844 | al %= ctxt->src.val; |
| 2845 | |
| 2846 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 2847 | |
| 2848 | /* Set PF, ZF, SF */ |
| 2849 | ctxt->src.type = OP_IMM; |
| 2850 | ctxt->src.val = 0; |
| 2851 | ctxt->src.bytes = 1; |
| 2852 | fastop(ctxt, em_or); |
| 2853 | |
| 2854 | return X86EMUL_CONTINUE; |
| 2855 | } |
| 2856 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2857 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2858 | { |
| 2859 | u8 al = ctxt->dst.val & 0xff; |
| 2860 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2861 | |
| 2862 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2863 | |
| 2864 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2865 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 2866 | /* Set PF, ZF, SF */ |
| 2867 | ctxt->src.type = OP_IMM; |
| 2868 | ctxt->src.val = 0; |
| 2869 | ctxt->src.bytes = 1; |
| 2870 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2871 | |
| 2872 | return X86EMUL_CONTINUE; |
| 2873 | } |
| 2874 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 2875 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 2876 | { |
| 2877 | long rel = ctxt->src.val; |
| 2878 | |
| 2879 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 2880 | jmp_rel(ctxt, rel); |
| 2881 | return em_push(ctxt); |
| 2882 | } |
| 2883 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2884 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2885 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2886 | u16 sel, old_cs; |
| 2887 | ulong old_eip; |
| 2888 | int rc; |
| 2889 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2890 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2891 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2892 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2893 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2894 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2895 | return X86EMUL_CONTINUE; |
| 2896 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2897 | ctxt->_eip = 0; |
| 2898 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2899 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2900 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2901 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2902 | if (rc != X86EMUL_CONTINUE) |
| 2903 | return rc; |
| 2904 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2905 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2906 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2907 | } |
| 2908 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2909 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2910 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2911 | int rc; |
| 2912 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2913 | ctxt->dst.type = OP_REG; |
| 2914 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2915 | ctxt->dst.bytes = ctxt->op_bytes; |
| 2916 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2917 | if (rc != X86EMUL_CONTINUE) |
| 2918 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 2919 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2920 | return X86EMUL_CONTINUE; |
| 2921 | } |
| 2922 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2923 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 2924 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2925 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2926 | ctxt->src.val = ctxt->dst.val; |
| 2927 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2928 | |
| 2929 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2930 | ctxt->dst.val = ctxt->src.orig_val; |
| 2931 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2932 | return X86EMUL_CONTINUE; |
| 2933 | } |
| 2934 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2935 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2936 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2937 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 2938 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2939 | } |
| 2940 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2941 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2942 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2943 | ctxt->dst.type = OP_REG; |
| 2944 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2945 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2946 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2947 | |
| 2948 | return X86EMUL_CONTINUE; |
| 2949 | } |
| 2950 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2951 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2952 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2953 | u64 tsc = 0; |
| 2954 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2955 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2956 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 2957 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2958 | return X86EMUL_CONTINUE; |
| 2959 | } |
| 2960 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2961 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 2962 | { |
| 2963 | u64 pmc; |
| 2964 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2965 | 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] | 2966 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2967 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 2968 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2969 | return X86EMUL_CONTINUE; |
| 2970 | } |
| 2971 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2972 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 2973 | { |
Paolo Bonzini | 54cfdb3 | 2014-03-27 11:36:25 +0100 | [diff] [blame] | 2974 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr)); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2975 | return X86EMUL_CONTINUE; |
| 2976 | } |
| 2977 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 2978 | #define FFL(x) bit(X86_FEATURE_##x) |
| 2979 | |
| 2980 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 2981 | { |
| 2982 | u32 ebx, ecx, edx, eax = 1; |
| 2983 | u16 tmp; |
| 2984 | |
| 2985 | /* |
| 2986 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 2987 | */ |
| 2988 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2989 | if (!(ecx & FFL(MOVBE))) |
| 2990 | return emulate_ud(ctxt); |
| 2991 | |
| 2992 | switch (ctxt->op_bytes) { |
| 2993 | case 2: |
| 2994 | /* |
| 2995 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 2996 | * the upper word of the destination register remains unchanged |
| 2997 | * ..." |
| 2998 | * |
| 2999 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3000 | * rules so we have to do the operation almost per hand. |
| 3001 | */ |
| 3002 | tmp = (u16)ctxt->src.val; |
| 3003 | ctxt->dst.val &= ~0xffffUL; |
| 3004 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3005 | break; |
| 3006 | case 4: |
| 3007 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3008 | break; |
| 3009 | case 8: |
| 3010 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3011 | break; |
| 3012 | default: |
| 3013 | return X86EMUL_PROPAGATE_FAULT; |
| 3014 | } |
| 3015 | return X86EMUL_CONTINUE; |
| 3016 | } |
| 3017 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3018 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3019 | { |
| 3020 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3021 | return emulate_gp(ctxt, 0); |
| 3022 | |
| 3023 | /* Disable writeback. */ |
| 3024 | ctxt->dst.type = OP_NONE; |
| 3025 | return X86EMUL_CONTINUE; |
| 3026 | } |
| 3027 | |
| 3028 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3029 | { |
| 3030 | unsigned long val; |
| 3031 | |
| 3032 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3033 | val = ctxt->src.val & ~0ULL; |
| 3034 | else |
| 3035 | val = ctxt->src.val & ~0U; |
| 3036 | |
| 3037 | /* #UD condition is already handled. */ |
| 3038 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3039 | return emulate_gp(ctxt, 0); |
| 3040 | |
| 3041 | /* Disable writeback. */ |
| 3042 | ctxt->dst.type = OP_NONE; |
| 3043 | return X86EMUL_CONTINUE; |
| 3044 | } |
| 3045 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3046 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3047 | { |
| 3048 | u64 msr_data; |
| 3049 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3050 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3051 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3052 | 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] | 3053 | return emulate_gp(ctxt, 0); |
| 3054 | |
| 3055 | return X86EMUL_CONTINUE; |
| 3056 | } |
| 3057 | |
| 3058 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3059 | { |
| 3060 | u64 msr_data; |
| 3061 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3062 | 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] | 3063 | return emulate_gp(ctxt, 0); |
| 3064 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3065 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3066 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3067 | return X86EMUL_CONTINUE; |
| 3068 | } |
| 3069 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3070 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3071 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3072 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3073 | return emulate_ud(ctxt); |
| 3074 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3075 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3076 | return X86EMUL_CONTINUE; |
| 3077 | } |
| 3078 | |
| 3079 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3080 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3081 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3082 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3083 | 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] | 3084 | return emulate_ud(ctxt); |
| 3085 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3086 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3087 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3088 | |
| 3089 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3090 | ctxt->dst.type = OP_NONE; |
| 3091 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3092 | } |
| 3093 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3094 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3095 | { |
| 3096 | u16 sel = ctxt->src.val; |
| 3097 | |
| 3098 | /* Disable writeback. */ |
| 3099 | ctxt->dst.type = OP_NONE; |
| 3100 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3101 | } |
| 3102 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3103 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3104 | { |
| 3105 | u16 sel = ctxt->src.val; |
| 3106 | |
| 3107 | /* Disable writeback. */ |
| 3108 | ctxt->dst.type = OP_NONE; |
| 3109 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3110 | } |
| 3111 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3112 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3113 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3114 | int rc; |
| 3115 | ulong linear; |
| 3116 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3117 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3118 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3119 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3120 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3121 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3122 | return X86EMUL_CONTINUE; |
| 3123 | } |
| 3124 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3125 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3126 | { |
| 3127 | ulong cr0; |
| 3128 | |
| 3129 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3130 | cr0 &= ~X86_CR0_TS; |
| 3131 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3132 | return X86EMUL_CONTINUE; |
| 3133 | } |
| 3134 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3135 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3136 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3137 | int rc; |
| 3138 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3139 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3140 | return X86EMUL_UNHANDLEABLE; |
| 3141 | |
| 3142 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3143 | if (rc != X86EMUL_CONTINUE) |
| 3144 | return rc; |
| 3145 | |
| 3146 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3147 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3148 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3149 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3150 | return X86EMUL_CONTINUE; |
| 3151 | } |
| 3152 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3153 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3154 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3155 | struct desc_ptr *ptr)) |
| 3156 | { |
| 3157 | struct desc_ptr desc_ptr; |
| 3158 | |
| 3159 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3160 | ctxt->op_bytes = 8; |
| 3161 | get(ctxt, &desc_ptr); |
| 3162 | if (ctxt->op_bytes == 2) { |
| 3163 | ctxt->op_bytes = 4; |
| 3164 | desc_ptr.address &= 0x00ffffff; |
| 3165 | } |
| 3166 | /* Disable writeback. */ |
| 3167 | ctxt->dst.type = OP_NONE; |
| 3168 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3169 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3170 | } |
| 3171 | |
| 3172 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3173 | { |
| 3174 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3175 | } |
| 3176 | |
| 3177 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3178 | { |
| 3179 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3180 | } |
| 3181 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3182 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3183 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3184 | struct desc_ptr desc_ptr; |
| 3185 | int rc; |
| 3186 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3187 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3188 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3189 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3190 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3191 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3192 | if (rc != X86EMUL_CONTINUE) |
| 3193 | return rc; |
| 3194 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3195 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3196 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3197 | return X86EMUL_CONTINUE; |
| 3198 | } |
| 3199 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3200 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3201 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3202 | int rc; |
| 3203 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3204 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3205 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3206 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3207 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3208 | return rc; |
| 3209 | } |
| 3210 | |
| 3211 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3212 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3213 | struct desc_ptr desc_ptr; |
| 3214 | int rc; |
| 3215 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3216 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3217 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3218 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3219 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3220 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3221 | if (rc != X86EMUL_CONTINUE) |
| 3222 | return rc; |
| 3223 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3224 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3225 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3226 | return X86EMUL_CONTINUE; |
| 3227 | } |
| 3228 | |
| 3229 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3230 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3231 | if (ctxt->dst.type == OP_MEM) |
| 3232 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3233 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3234 | return X86EMUL_CONTINUE; |
| 3235 | } |
| 3236 | |
| 3237 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3238 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3239 | 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] | 3240 | | (ctxt->src.val & 0x0f)); |
| 3241 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3242 | return X86EMUL_CONTINUE; |
| 3243 | } |
| 3244 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3245 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3246 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3247 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3248 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3249 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3250 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3251 | |
| 3252 | return X86EMUL_CONTINUE; |
| 3253 | } |
| 3254 | |
| 3255 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3256 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3257 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3258 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3259 | |
| 3260 | return X86EMUL_CONTINUE; |
| 3261 | } |
| 3262 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3263 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3264 | { |
| 3265 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3266 | &ctxt->dst.val)) |
| 3267 | return X86EMUL_IO_NEEDED; |
| 3268 | |
| 3269 | return X86EMUL_CONTINUE; |
| 3270 | } |
| 3271 | |
| 3272 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3273 | { |
| 3274 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3275 | &ctxt->src.val, 1); |
| 3276 | /* Disable writeback. */ |
| 3277 | ctxt->dst.type = OP_NONE; |
| 3278 | return X86EMUL_CONTINUE; |
| 3279 | } |
| 3280 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3281 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3282 | { |
| 3283 | if (emulator_bad_iopl(ctxt)) |
| 3284 | return emulate_gp(ctxt, 0); |
| 3285 | |
| 3286 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3287 | return X86EMUL_CONTINUE; |
| 3288 | } |
| 3289 | |
| 3290 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3291 | { |
| 3292 | if (emulator_bad_iopl(ctxt)) |
| 3293 | return emulate_gp(ctxt, 0); |
| 3294 | |
| 3295 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3296 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3297 | return X86EMUL_CONTINUE; |
| 3298 | } |
| 3299 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3300 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3301 | { |
| 3302 | u32 eax, ebx, ecx, edx; |
| 3303 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3304 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3305 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3306 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3307 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3308 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3309 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3310 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3311 | return X86EMUL_CONTINUE; |
| 3312 | } |
| 3313 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3314 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3315 | { |
| 3316 | u32 flags; |
| 3317 | |
| 3318 | flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF; |
| 3319 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3320 | |
| 3321 | ctxt->eflags &= ~0xffUL; |
| 3322 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3323 | return X86EMUL_CONTINUE; |
| 3324 | } |
| 3325 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3326 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3327 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3328 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3329 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3330 | return X86EMUL_CONTINUE; |
| 3331 | } |
| 3332 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3333 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3334 | { |
| 3335 | switch (ctxt->op_bytes) { |
| 3336 | #ifdef CONFIG_X86_64 |
| 3337 | case 8: |
| 3338 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3339 | break; |
| 3340 | #endif |
| 3341 | default: |
| 3342 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3343 | break; |
| 3344 | } |
| 3345 | return X86EMUL_CONTINUE; |
| 3346 | } |
| 3347 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3348 | static bool valid_cr(int nr) |
| 3349 | { |
| 3350 | switch (nr) { |
| 3351 | case 0: |
| 3352 | case 2 ... 4: |
| 3353 | case 8: |
| 3354 | return true; |
| 3355 | default: |
| 3356 | return false; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3361 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3362 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3363 | return emulate_ud(ctxt); |
| 3364 | |
| 3365 | return X86EMUL_CONTINUE; |
| 3366 | } |
| 3367 | |
| 3368 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3369 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3370 | u64 new_val = ctxt->src.val64; |
| 3371 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3372 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3373 | |
| 3374 | static u64 cr_reserved_bits[] = { |
| 3375 | 0xffffffff00000000ULL, |
| 3376 | 0, 0, 0, /* CR3 checked later */ |
| 3377 | CR4_RESERVED_BITS, |
| 3378 | 0, 0, 0, |
| 3379 | CR8_RESERVED_BITS, |
| 3380 | }; |
| 3381 | |
| 3382 | if (!valid_cr(cr)) |
| 3383 | return emulate_ud(ctxt); |
| 3384 | |
| 3385 | if (new_val & cr_reserved_bits[cr]) |
| 3386 | return emulate_gp(ctxt, 0); |
| 3387 | |
| 3388 | switch (cr) { |
| 3389 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3390 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3391 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3392 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3393 | return emulate_gp(ctxt, 0); |
| 3394 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3395 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3396 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3397 | |
| 3398 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3399 | !(cr4 & X86_CR4_PAE)) |
| 3400 | return emulate_gp(ctxt, 0); |
| 3401 | |
| 3402 | break; |
| 3403 | } |
| 3404 | case 3: { |
| 3405 | u64 rsvd = 0; |
| 3406 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3407 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3408 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3409 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3410 | |
| 3411 | if (new_val & rsvd) |
| 3412 | return emulate_gp(ctxt, 0); |
| 3413 | |
| 3414 | break; |
| 3415 | } |
| 3416 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3417 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3418 | |
| 3419 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3420 | return emulate_gp(ctxt, 0); |
| 3421 | |
| 3422 | break; |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | return X86EMUL_CONTINUE; |
| 3427 | } |
| 3428 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3429 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3430 | { |
| 3431 | unsigned long dr7; |
| 3432 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3433 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3434 | |
| 3435 | /* Check if DR7.Global_Enable is set */ |
| 3436 | return dr7 & (1 << 13); |
| 3437 | } |
| 3438 | |
| 3439 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3440 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3441 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3442 | u64 cr4; |
| 3443 | |
| 3444 | if (dr > 7) |
| 3445 | return emulate_ud(ctxt); |
| 3446 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3447 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3448 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3449 | return emulate_ud(ctxt); |
| 3450 | |
| 3451 | if (check_dr7_gd(ctxt)) |
| 3452 | return emulate_db(ctxt); |
| 3453 | |
| 3454 | return X86EMUL_CONTINUE; |
| 3455 | } |
| 3456 | |
| 3457 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3458 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3459 | u64 new_val = ctxt->src.val64; |
| 3460 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3461 | |
| 3462 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3463 | return emulate_gp(ctxt, 0); |
| 3464 | |
| 3465 | return check_dr_read(ctxt); |
| 3466 | } |
| 3467 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3468 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3469 | { |
| 3470 | u64 efer; |
| 3471 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3472 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3473 | |
| 3474 | if (!(efer & EFER_SVME)) |
| 3475 | return emulate_ud(ctxt); |
| 3476 | |
| 3477 | return X86EMUL_CONTINUE; |
| 3478 | } |
| 3479 | |
| 3480 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3481 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3482 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3485 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3486 | return emulate_gp(ctxt, 0); |
| 3487 | |
| 3488 | return check_svme(ctxt); |
| 3489 | } |
| 3490 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3491 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3492 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3493 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3494 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3495 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3496 | return emulate_ud(ctxt); |
| 3497 | |
| 3498 | return X86EMUL_CONTINUE; |
| 3499 | } |
| 3500 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3501 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3502 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3503 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3504 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3505 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3506 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3507 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3508 | return emulate_gp(ctxt, 0); |
| 3509 | |
| 3510 | return X86EMUL_CONTINUE; |
| 3511 | } |
| 3512 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3513 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3514 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3515 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3516 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3517 | return emulate_gp(ctxt, 0); |
| 3518 | |
| 3519 | return X86EMUL_CONTINUE; |
| 3520 | } |
| 3521 | |
| 3522 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3523 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3524 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3525 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3526 | return emulate_gp(ctxt, 0); |
| 3527 | |
| 3528 | return X86EMUL_CONTINUE; |
| 3529 | } |
| 3530 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3531 | #define D(_y) { .flags = (_y) } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3532 | #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i } |
| 3533 | #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \ |
| 3534 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3535 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3536 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3537 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3538 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3539 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3540 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3541 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3542 | #define II(_f, _e, _i) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3543 | { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3544 | #define IIP(_f, _e, _i, _p) \ |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 3545 | { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \ |
| 3546 | .intercept = x86_intercept_##_i, .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3547 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3548 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3549 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3550 | #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] | 3551 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3552 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3553 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3554 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3555 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3556 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3557 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3558 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3559 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3560 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3561 | DI(SrcNone | Priv, monitor), |
| 3562 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3563 | N, N, N, N, N, N, |
| 3564 | }; |
| 3565 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3566 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3567 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3568 | II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3569 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3570 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3571 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3572 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3573 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3574 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3575 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3576 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3577 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3578 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3579 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3580 | N, N, N, N, N, N, |
| 3581 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3582 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3583 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3584 | F(Lock, em_add), |
| 3585 | F(Lock | PageTable, em_or), |
| 3586 | F(Lock, em_adc), |
| 3587 | F(Lock, em_sbb), |
| 3588 | F(Lock | PageTable, em_and), |
| 3589 | F(Lock, em_sub), |
| 3590 | F(Lock, em_xor), |
| 3591 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3592 | }; |
| 3593 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3594 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3595 | 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] | 3596 | }; |
| 3597 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3598 | static const struct opcode group2[] = { |
| 3599 | F(DstMem | ModRM, em_rol), |
| 3600 | F(DstMem | ModRM, em_ror), |
| 3601 | F(DstMem | ModRM, em_rcl), |
| 3602 | F(DstMem | ModRM, em_rcr), |
| 3603 | F(DstMem | ModRM, em_shl), |
| 3604 | F(DstMem | ModRM, em_shr), |
| 3605 | F(DstMem | ModRM, em_shl), |
| 3606 | F(DstMem | ModRM, em_sar), |
| 3607 | }; |
| 3608 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3609 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3610 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3611 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3612 | F(DstMem | SrcNone | Lock, em_not), |
| 3613 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3614 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3615 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3616 | F(DstXacc | Src2Mem, em_div_ex), |
| 3617 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3618 | }; |
| 3619 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3620 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3621 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3622 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3623 | N, N, N, N, N, N, |
| 3624 | }; |
| 3625 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3626 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3627 | F(DstMem | SrcNone | Lock, em_inc), |
| 3628 | F(DstMem | SrcNone | Lock, em_dec), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3629 | I(SrcMem | Stack, em_grp45), |
| 3630 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3631 | I(SrcMem | Stack, em_grp45), |
| 3632 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
Gleb Natapov | 188424b | 2013-04-11 12:32:14 +0300 | [diff] [blame] | 3633 | I(SrcMem | Stack, em_grp45), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3634 | }; |
| 3635 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3636 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3637 | DI(Prot, sldt), |
| 3638 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3639 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3640 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3641 | N, N, N, N, |
| 3642 | }; |
| 3643 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3644 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3645 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3646 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3647 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3648 | II(SrcMem | Priv, em_lidt, lidt), |
| 3649 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3650 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3651 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3652 | }, { |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3653 | I(SrcNone | Priv | EmulateOnUD, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3654 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3655 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3656 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3657 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3658 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3659 | } }; |
| 3660 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3661 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3662 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3663 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3664 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3665 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3666 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3667 | }; |
| 3668 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3669 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3670 | 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] | 3671 | }, { |
| 3672 | N, N, N, N, N, N, N, N, |
| 3673 | } }; |
| 3674 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3675 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3676 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3677 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3678 | }; |
| 3679 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3680 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3681 | 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] | 3682 | }; |
| 3683 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3684 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3685 | I(0, em_mov), N, N, N, |
| 3686 | }; |
| 3687 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3688 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3689 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3690 | }; |
| 3691 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3692 | static const struct escape escape_d9 = { { |
| 3693 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3694 | }, { |
| 3695 | /* 0xC0 - 0xC7 */ |
| 3696 | N, N, N, N, N, N, N, N, |
| 3697 | /* 0xC8 - 0xCF */ |
| 3698 | N, N, N, N, N, N, N, N, |
| 3699 | /* 0xD0 - 0xC7 */ |
| 3700 | N, N, N, N, N, N, N, N, |
| 3701 | /* 0xD8 - 0xDF */ |
| 3702 | N, N, N, N, N, N, N, N, |
| 3703 | /* 0xE0 - 0xE7 */ |
| 3704 | N, N, N, N, N, N, N, N, |
| 3705 | /* 0xE8 - 0xEF */ |
| 3706 | N, N, N, N, N, N, N, N, |
| 3707 | /* 0xF0 - 0xF7 */ |
| 3708 | N, N, N, N, N, N, N, N, |
| 3709 | /* 0xF8 - 0xFF */ |
| 3710 | N, N, N, N, N, N, N, N, |
| 3711 | } }; |
| 3712 | |
| 3713 | static const struct escape escape_db = { { |
| 3714 | N, N, N, N, N, N, N, N, |
| 3715 | }, { |
| 3716 | /* 0xC0 - 0xC7 */ |
| 3717 | N, N, N, N, N, N, N, N, |
| 3718 | /* 0xC8 - 0xCF */ |
| 3719 | N, N, N, N, N, N, N, N, |
| 3720 | /* 0xD0 - 0xC7 */ |
| 3721 | N, N, N, N, N, N, N, N, |
| 3722 | /* 0xD8 - 0xDF */ |
| 3723 | N, N, N, N, N, N, N, N, |
| 3724 | /* 0xE0 - 0xE7 */ |
| 3725 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3726 | /* 0xE8 - 0xEF */ |
| 3727 | N, N, N, N, N, N, N, N, |
| 3728 | /* 0xF0 - 0xF7 */ |
| 3729 | N, N, N, N, N, N, N, N, |
| 3730 | /* 0xF8 - 0xFF */ |
| 3731 | N, N, N, N, N, N, N, N, |
| 3732 | } }; |
| 3733 | |
| 3734 | static const struct escape escape_dd = { { |
| 3735 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3736 | }, { |
| 3737 | /* 0xC0 - 0xC7 */ |
| 3738 | N, N, N, N, N, N, N, N, |
| 3739 | /* 0xC8 - 0xCF */ |
| 3740 | N, N, N, N, N, N, N, N, |
| 3741 | /* 0xD0 - 0xC7 */ |
| 3742 | N, N, N, N, N, N, N, N, |
| 3743 | /* 0xD8 - 0xDF */ |
| 3744 | N, N, N, N, N, N, N, N, |
| 3745 | /* 0xE0 - 0xE7 */ |
| 3746 | N, N, N, N, N, N, N, N, |
| 3747 | /* 0xE8 - 0xEF */ |
| 3748 | N, N, N, N, N, N, N, N, |
| 3749 | /* 0xF0 - 0xF7 */ |
| 3750 | N, N, N, N, N, N, N, N, |
| 3751 | /* 0xF8 - 0xFF */ |
| 3752 | N, N, N, N, N, N, N, N, |
| 3753 | } }; |
| 3754 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3755 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3756 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3757 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3758 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3759 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3760 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3761 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3762 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3763 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3764 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3765 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3766 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3767 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3768 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3769 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3770 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3771 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3772 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3773 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3774 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3775 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3776 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3777 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3778 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3779 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3780 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3781 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3782 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3783 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3784 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3785 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3786 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3787 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3788 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3789 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3790 | N, N, N, N, |
| 3791 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3792 | I(SrcImm | Mov | Stack, em_push), |
| 3793 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3794 | I(SrcImmByte | Mov | Stack, em_push), |
| 3795 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3796 | 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] | 3797 | 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] | 3798 | /* 0x70 - 0x7F */ |
| 3799 | X16(D(SrcImmByte)), |
| 3800 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3801 | G(ByteOp | DstMem | SrcImm, group1), |
| 3802 | G(DstMem | SrcImm, group1), |
| 3803 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3804 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3805 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3806 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3807 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3808 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3809 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3810 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3811 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3812 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3813 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3814 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3815 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3816 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3817 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3818 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3819 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3820 | II(ImplicitOps | Stack, em_popf, popf), |
| 3821 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3822 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3823 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3824 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3825 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3826 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3827 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3828 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3829 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3830 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3831 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3832 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3833 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3834 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3835 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3836 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3837 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3838 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3839 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3840 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3841 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3842 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3843 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3844 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 3845 | I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm), |
| 3846 | I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3847 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3848 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3849 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3850 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 3851 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3852 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 3853 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 3854 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 3855 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3856 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3857 | 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] | 3858 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3859 | X3(I(SrcImmByte, em_loop)), |
| 3860 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3861 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3862 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3863 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3864 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3865 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3866 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3867 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3868 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3869 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3870 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3871 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3872 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3873 | D(ImplicitOps), D(ImplicitOps), |
| 3874 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3875 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3876 | }; |
| 3877 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3878 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3879 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3880 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3881 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3882 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3883 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3884 | N, D(ImplicitOps | ModRM), N, N, |
| 3885 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 3886 | N, N, N, N, N, N, N, N, |
| 3887 | D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3888 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 3889 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 3890 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 3891 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 3892 | check_cr_write), |
| 3893 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 3894 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3895 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3896 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 3897 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
| 3898 | N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3899 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3900 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3901 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3902 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3903 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3904 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3905 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 3906 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 3907 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3908 | N, N, N, N, N, N, N, N, |
| 3909 | /* 0x40 - 0x4F */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 3910 | X16(D(DstReg | SrcMem | ModRM)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3911 | /* 0x50 - 0x5F */ |
| 3912 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3913 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3914 | N, N, N, N, |
| 3915 | N, N, N, N, |
| 3916 | N, N, N, N, |
| 3917 | 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] | 3918 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3919 | N, N, N, N, |
| 3920 | N, N, N, N, |
| 3921 | N, N, N, N, |
| 3922 | 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] | 3923 | /* 0x80 - 0x8F */ |
| 3924 | X16(D(SrcImm)), |
| 3925 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3926 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3927 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3928 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3929 | II(ImplicitOps, em_cpuid, cpuid), |
| 3930 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3931 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 3932 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3933 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3934 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3935 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3936 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3937 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 3938 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3939 | D(ModRM), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3940 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 3941 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3942 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3943 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3944 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 3945 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3946 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3947 | /* 0xB8 - 0xBF */ |
| 3948 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3949 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3950 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 3951 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3952 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3953 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 3954 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3955 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3956 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3957 | /* 0xC8 - 0xCF */ |
| 3958 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3959 | /* 0xD0 - 0xDF */ |
| 3960 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3961 | /* 0xE0 - 0xEF */ |
| 3962 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3963 | /* 0xF0 - 0xFF */ |
| 3964 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 3965 | }; |
| 3966 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3967 | static const struct gprefix three_byte_0f_38_f0 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3968 | I(DstReg | SrcMem | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3969 | }; |
| 3970 | |
| 3971 | static const struct gprefix three_byte_0f_38_f1 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3972 | I(DstMem | SrcReg | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3973 | }; |
| 3974 | |
| 3975 | /* |
| 3976 | * Insns below are selected by the prefix which indexed by the third opcode |
| 3977 | * byte. |
| 3978 | */ |
| 3979 | static const struct opcode opcode_map_0f_38[256] = { |
| 3980 | /* 0x00 - 0x7f */ |
| 3981 | 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] | 3982 | /* 0x80 - 0xef */ |
| 3983 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 3984 | /* 0xf0 - 0xf1 */ |
| 3985 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0), |
| 3986 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1), |
| 3987 | /* 0xf2 - 0xff */ |
| 3988 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3989 | }; |
| 3990 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3991 | #undef D |
| 3992 | #undef N |
| 3993 | #undef G |
| 3994 | #undef GD |
| 3995 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3996 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3997 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3998 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3999 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4000 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4001 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4002 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4003 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4004 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4005 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4006 | { |
| 4007 | unsigned size; |
| 4008 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4009 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4010 | if (size == 8) |
| 4011 | size = 4; |
| 4012 | return size; |
| 4013 | } |
| 4014 | |
| 4015 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4016 | unsigned size, bool sign_extension) |
| 4017 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4018 | int rc = X86EMUL_CONTINUE; |
| 4019 | |
| 4020 | op->type = OP_IMM; |
| 4021 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4022 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4023 | /* NB. Immediates are sign-extended as necessary. */ |
| 4024 | switch (op->bytes) { |
| 4025 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4026 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4027 | break; |
| 4028 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4029 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4030 | break; |
| 4031 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4032 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4033 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4034 | case 8: |
| 4035 | op->val = insn_fetch(s64, ctxt); |
| 4036 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4037 | } |
| 4038 | if (!sign_extension) { |
| 4039 | switch (op->bytes) { |
| 4040 | case 1: |
| 4041 | op->val &= 0xff; |
| 4042 | break; |
| 4043 | case 2: |
| 4044 | op->val &= 0xffff; |
| 4045 | break; |
| 4046 | case 4: |
| 4047 | op->val &= 0xffffffff; |
| 4048 | break; |
| 4049 | } |
| 4050 | } |
| 4051 | done: |
| 4052 | return rc; |
| 4053 | } |
| 4054 | |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4055 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4056 | unsigned d) |
| 4057 | { |
| 4058 | int rc = X86EMUL_CONTINUE; |
| 4059 | |
| 4060 | switch (d) { |
| 4061 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4062 | decode_register_operand(ctxt, op); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4063 | break; |
| 4064 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4065 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4066 | break; |
| 4067 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4068 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4069 | mem_common: |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4070 | *op = ctxt->memop; |
| 4071 | ctxt->memopp = op; |
Paolo Bonzini | 9688897 | 2014-04-01 14:54:19 +0200 | [diff] [blame] | 4072 | if (ctxt->d & BitOp) |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4073 | fetch_bit_operand(ctxt); |
| 4074 | op->orig_val = op->val; |
| 4075 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4076 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4077 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4078 | goto mem_common; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4079 | case OpAcc: |
| 4080 | op->type = OP_REG; |
| 4081 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4082 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4083 | fetch_register_operand(op); |
| 4084 | op->orig_val = op->val; |
| 4085 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4086 | case OpAccLo: |
| 4087 | op->type = OP_REG; |
| 4088 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4089 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4090 | fetch_register_operand(op); |
| 4091 | op->orig_val = op->val; |
| 4092 | break; |
| 4093 | case OpAccHi: |
| 4094 | if (ctxt->d & ByteOp) { |
| 4095 | op->type = OP_NONE; |
| 4096 | break; |
| 4097 | } |
| 4098 | op->type = OP_REG; |
| 4099 | op->bytes = ctxt->op_bytes; |
| 4100 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4101 | fetch_register_operand(op); |
| 4102 | op->orig_val = op->val; |
| 4103 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4104 | case OpDI: |
| 4105 | op->type = OP_MEM; |
| 4106 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4107 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4108 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4109 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4110 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4111 | op->count = 1; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4112 | break; |
| 4113 | case OpDX: |
| 4114 | op->type = OP_REG; |
| 4115 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4116 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4117 | fetch_register_operand(op); |
| 4118 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4119 | case OpCL: |
| 4120 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4121 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4122 | break; |
| 4123 | case OpImmByte: |
| 4124 | rc = decode_imm(ctxt, op, 1, true); |
| 4125 | break; |
| 4126 | case OpOne: |
| 4127 | op->bytes = 1; |
| 4128 | op->val = 1; |
| 4129 | break; |
| 4130 | case OpImm: |
| 4131 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4132 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4133 | case OpImm64: |
| 4134 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4135 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4136 | case OpMem8: |
| 4137 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4138 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4139 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4140 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4141 | fetch_register_operand(&ctxt->memop); |
| 4142 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4143 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4144 | case OpMem16: |
| 4145 | ctxt->memop.bytes = 2; |
| 4146 | goto mem_common; |
| 4147 | case OpMem32: |
| 4148 | ctxt->memop.bytes = 4; |
| 4149 | goto mem_common; |
| 4150 | case OpImmU16: |
| 4151 | rc = decode_imm(ctxt, op, 2, false); |
| 4152 | break; |
| 4153 | case OpImmU: |
| 4154 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4155 | break; |
| 4156 | case OpSI: |
| 4157 | op->type = OP_MEM; |
| 4158 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4159 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4160 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4161 | op->addr.mem.seg = ctxt->seg_override; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4162 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4163 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4164 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4165 | case OpXLat: |
| 4166 | op->type = OP_MEM; |
| 4167 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4168 | op->addr.mem.ea = |
| 4169 | register_address(ctxt, |
| 4170 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4171 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4172 | op->addr.mem.seg = ctxt->seg_override; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4173 | op->val = 0; |
| 4174 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4175 | case OpImmFAddr: |
| 4176 | op->type = OP_IMM; |
| 4177 | op->addr.mem.ea = ctxt->_eip; |
| 4178 | op->bytes = ctxt->op_bytes + 2; |
| 4179 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4180 | break; |
| 4181 | case OpMemFAddr: |
| 4182 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4183 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4184 | case OpES: |
| 4185 | op->val = VCPU_SREG_ES; |
| 4186 | break; |
| 4187 | case OpCS: |
| 4188 | op->val = VCPU_SREG_CS; |
| 4189 | break; |
| 4190 | case OpSS: |
| 4191 | op->val = VCPU_SREG_SS; |
| 4192 | break; |
| 4193 | case OpDS: |
| 4194 | op->val = VCPU_SREG_DS; |
| 4195 | break; |
| 4196 | case OpFS: |
| 4197 | op->val = VCPU_SREG_FS; |
| 4198 | break; |
| 4199 | case OpGS: |
| 4200 | op->val = VCPU_SREG_GS; |
| 4201 | break; |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4202 | case OpImplicit: |
| 4203 | /* Special instructions do their own operand decoding. */ |
| 4204 | default: |
| 4205 | op->type = OP_NONE; /* Disable writeback. */ |
| 4206 | break; |
| 4207 | } |
| 4208 | |
| 4209 | done: |
| 4210 | return rc; |
| 4211 | } |
| 4212 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4213 | 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] | 4214 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4215 | int rc = X86EMUL_CONTINUE; |
| 4216 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4217 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4218 | bool op_prefix = false; |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4219 | bool has_seg_override = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4220 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4221 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4222 | ctxt->memop.type = OP_NONE; |
| 4223 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4224 | ctxt->_eip = ctxt->eip; |
| 4225 | ctxt->fetch.start = ctxt->_eip; |
| 4226 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4227 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4228 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4229 | memcpy(ctxt->fetch.data, insn, insn_len); |
Paolo Bonzini | 285ca9e | 2014-05-06 12:24:32 +0200 | [diff] [blame^] | 4230 | else { |
| 4231 | rc = do_insn_fetch_bytes(ctxt); |
| 4232 | if (rc != X86EMUL_CONTINUE) |
| 4233 | return rc; |
| 4234 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4235 | |
| 4236 | switch (mode) { |
| 4237 | case X86EMUL_MODE_REAL: |
| 4238 | case X86EMUL_MODE_VM86: |
| 4239 | case X86EMUL_MODE_PROT16: |
| 4240 | def_op_bytes = def_ad_bytes = 2; |
| 4241 | break; |
| 4242 | case X86EMUL_MODE_PROT32: |
| 4243 | def_op_bytes = def_ad_bytes = 4; |
| 4244 | break; |
| 4245 | #ifdef CONFIG_X86_64 |
| 4246 | case X86EMUL_MODE_PROT64: |
| 4247 | def_op_bytes = 4; |
| 4248 | def_ad_bytes = 8; |
| 4249 | break; |
| 4250 | #endif |
| 4251 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4252 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4253 | } |
| 4254 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4255 | ctxt->op_bytes = def_op_bytes; |
| 4256 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4257 | |
| 4258 | /* Legacy prefixes. */ |
| 4259 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4260 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4261 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4262 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4263 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4264 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4265 | break; |
| 4266 | case 0x67: /* address-size override */ |
| 4267 | if (mode == X86EMUL_MODE_PROT64) |
| 4268 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4269 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4270 | else |
| 4271 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4272 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4273 | break; |
| 4274 | case 0x26: /* ES override */ |
| 4275 | case 0x2e: /* CS override */ |
| 4276 | case 0x36: /* SS override */ |
| 4277 | case 0x3e: /* DS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4278 | has_seg_override = true; |
| 4279 | ctxt->seg_override = (ctxt->b >> 3) & 3; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4280 | break; |
| 4281 | case 0x64: /* FS override */ |
| 4282 | case 0x65: /* GS override */ |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4283 | has_seg_override = true; |
| 4284 | ctxt->seg_override = ctxt->b & 7; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4285 | break; |
| 4286 | case 0x40 ... 0x4f: /* REX */ |
| 4287 | if (mode != X86EMUL_MODE_PROT64) |
| 4288 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4289 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4290 | continue; |
| 4291 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4292 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4293 | break; |
| 4294 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4295 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4296 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4297 | break; |
| 4298 | default: |
| 4299 | goto done_prefixes; |
| 4300 | } |
| 4301 | |
| 4302 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4303 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4304 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | done_prefixes: |
| 4308 | |
| 4309 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4310 | if (ctxt->rex_prefix & 8) |
| 4311 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4312 | |
| 4313 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4314 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4315 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4316 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4317 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4318 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4319 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4320 | |
| 4321 | /* 0F_38 opcode map */ |
| 4322 | if (ctxt->b == 0x38) { |
| 4323 | ctxt->opcode_len = 3; |
| 4324 | ctxt->b = insn_fetch(u8, ctxt); |
| 4325 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4326 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4327 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4328 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4329 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4330 | if (ctxt->d & ModRM) |
| 4331 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4332 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4333 | /* vex-prefix instructions are not implemented */ |
| 4334 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
| 4335 | (mode == X86EMUL_MODE_PROT64 || |
| 4336 | (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) { |
| 4337 | ctxt->d = NotImpl; |
| 4338 | } |
| 4339 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4340 | while (ctxt->d & GroupMask) { |
| 4341 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4342 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4343 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4344 | opcode = opcode.u.group[goffset]; |
| 4345 | break; |
| 4346 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4347 | goffset = (ctxt->modrm >> 3) & 7; |
| 4348 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4349 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4350 | else |
| 4351 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4352 | break; |
| 4353 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4354 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4355 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4356 | break; |
| 4357 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4358 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4359 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4360 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4361 | switch (simd_prefix) { |
| 4362 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4363 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4364 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4365 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4366 | } |
| 4367 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4368 | case Escape: |
| 4369 | if (ctxt->modrm > 0xbf) |
| 4370 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4371 | else |
| 4372 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4373 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4374 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4375 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4376 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4377 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4378 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4379 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
Paolo Bonzini | e24186e | 2014-03-27 12:00:57 +0100 | [diff] [blame] | 4382 | /* Unrecognised? */ |
| 4383 | if (ctxt->d == 0) |
| 4384 | return EMULATION_FAILED; |
| 4385 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4386 | ctxt->execute = opcode.u.execute; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4387 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4388 | if (unlikely(ctxt->d & |
| 4389 | (NotImpl|EmulateOnUD|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm))) { |
| 4390 | /* |
| 4391 | * These are copied unconditionally here, and checked unconditionally |
| 4392 | * in x86_emulate_insn. |
| 4393 | */ |
| 4394 | ctxt->check_perm = opcode.check_perm; |
| 4395 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4396 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4397 | if (ctxt->d & NotImpl) |
| 4398 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4399 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4400 | if (!(ctxt->d & EmulateOnUD) && ctxt->ud) |
| 4401 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4402 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4403 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4404 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4405 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4406 | if (ctxt->d & Op3264) { |
| 4407 | if (mode == X86EMUL_MODE_PROT64) |
| 4408 | ctxt->op_bytes = 8; |
| 4409 | else |
| 4410 | ctxt->op_bytes = 4; |
| 4411 | } |
| 4412 | |
| 4413 | if (ctxt->d & Sse) |
| 4414 | ctxt->op_bytes = 16; |
| 4415 | else if (ctxt->d & Mmx) |
| 4416 | ctxt->op_bytes = 8; |
| 4417 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4418 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4419 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4420 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4421 | rc = decode_modrm(ctxt, &ctxt->memop); |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4422 | if (!has_seg_override) { |
| 4423 | has_seg_override = true; |
| 4424 | ctxt->seg_override = ctxt->modrm_seg; |
| 4425 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4426 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4427 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4428 | if (rc != X86EMUL_CONTINUE) |
| 4429 | goto done; |
| 4430 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4431 | if (!has_seg_override) |
| 4432 | ctxt->seg_override = VCPU_SREG_DS; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4433 | |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4434 | ctxt->memop.addr.mem.seg = ctxt->seg_override; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4435 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4436 | /* |
| 4437 | * Decode and fetch the source operand: register, memory |
| 4438 | * or immediate. |
| 4439 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4440 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4441 | if (rc != X86EMUL_CONTINUE) |
| 4442 | goto done; |
| 4443 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4444 | /* |
| 4445 | * Decode and fetch the second source operand: register, memory |
| 4446 | * or immediate. |
| 4447 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4448 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4449 | if (rc != X86EMUL_CONTINUE) |
| 4450 | goto done; |
| 4451 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4452 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a994554 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4453 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4454 | |
| 4455 | done: |
Bandan Das | 41061cd | 2014-04-16 12:46:14 -0400 | [diff] [blame] | 4456 | if (ctxt->rip_relative) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4457 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4458 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4459 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4460 | } |
| 4461 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4462 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4463 | { |
| 4464 | return ctxt->d & PageTable; |
| 4465 | } |
| 4466 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4467 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4468 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4469 | /* The second termination condition only applies for REPE |
| 4470 | * and REPNE. Test if the repeat string operation prefix is |
| 4471 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4472 | * corresponding termination condition according to: |
| 4473 | * - if REPE/REPZ and ZF = 0 then done |
| 4474 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4475 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4476 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4477 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4478 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4479 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4480 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4481 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4482 | return true; |
| 4483 | |
| 4484 | return false; |
| 4485 | } |
| 4486 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4487 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4488 | { |
| 4489 | bool fault = false; |
| 4490 | |
| 4491 | ctxt->ops->get_fpu(ctxt); |
| 4492 | asm volatile("1: fwait \n\t" |
| 4493 | "2: \n\t" |
| 4494 | ".pushsection .fixup,\"ax\" \n\t" |
| 4495 | "3: \n\t" |
| 4496 | "movb $1, %[fault] \n\t" |
| 4497 | "jmp 2b \n\t" |
| 4498 | ".popsection \n\t" |
| 4499 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4500 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4501 | ctxt->ops->put_fpu(ctxt); |
| 4502 | |
| 4503 | if (unlikely(fault)) |
| 4504 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4505 | |
| 4506 | return X86EMUL_CONTINUE; |
| 4507 | } |
| 4508 | |
| 4509 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4510 | struct operand *op) |
| 4511 | { |
| 4512 | if (op->type == OP_MM) |
| 4513 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4514 | } |
| 4515 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4516 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4517 | { |
| 4518 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4519 | if (!(ctxt->d & ByteOp)) |
| 4520 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4521 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4522 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4523 | [fastop]"+S"(fop) |
| 4524 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4525 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4526 | if (!fop) /* exception is returned in fop variable */ |
| 4527 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4528 | return X86EMUL_CONTINUE; |
| 4529 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4530 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4531 | void init_decode_cache(struct x86_emulate_ctxt *ctxt) |
| 4532 | { |
Bandan Das | 573e80f | 2014-04-16 12:46:13 -0400 | [diff] [blame] | 4533 | memset(&ctxt->rip_relative, 0, |
| 4534 | (void *)&ctxt->modrm - (void *)&ctxt->rip_relative); |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4535 | |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4536 | ctxt->io_read.pos = 0; |
| 4537 | ctxt->io_read.end = 0; |
Bandan Das | 1498507 | 2014-04-16 12:46:09 -0400 | [diff] [blame] | 4538 | ctxt->mem_read.end = 0; |
| 4539 | } |
| 4540 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4541 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4542 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4543 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4544 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4545 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4546 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4547 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4548 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4549 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4550 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4551 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4552 | goto done; |
| 4553 | } |
| 4554 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4555 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4556 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4557 | goto done; |
| 4558 | } |
| 4559 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4560 | if (unlikely(ctxt->d & |
| 4561 | (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) { |
| 4562 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4563 | (ctxt->d & Undefined)) { |
| 4564 | rc = emulate_ud(ctxt); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4565 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4566 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4567 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4568 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4569 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
| 4570 | rc = emulate_ud(ctxt); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4571 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4572 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4573 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4574 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
| 4575 | rc = emulate_nm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4576 | goto done; |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4577 | } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4578 | |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4579 | if (ctxt->d & Mmx) { |
| 4580 | rc = flush_pending_x87_faults(ctxt); |
| 4581 | if (rc != X86EMUL_CONTINUE) |
| 4582 | goto done; |
| 4583 | /* |
| 4584 | * Now that we know the fpu is exception safe, we can fetch |
| 4585 | * operands from it. |
| 4586 | */ |
| 4587 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4588 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4589 | if (!(ctxt->d & Mov)) |
| 4590 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4591 | } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4592 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4593 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4594 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4595 | X86_ICPT_PRE_EXCEPT); |
| 4596 | if (rc != X86EMUL_CONTINUE) |
| 4597 | goto done; |
| 4598 | } |
| 4599 | |
| 4600 | /* Privileged instruction can be executed only in CPL=0 */ |
| 4601 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
| 4602 | rc = emulate_gp(ctxt, 0); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4603 | goto done; |
| 4604 | } |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Instruction can only be executed in protected mode */ |
| 4607 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
| 4608 | rc = emulate_ud(ctxt); |
| 4609 | goto done; |
| 4610 | } |
| 4611 | |
| 4612 | /* Do instruction specific permission checks */ |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4613 | if (ctxt->d & CheckPerm) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4614 | rc = ctxt->check_perm(ctxt); |
| 4615 | if (rc != X86EMUL_CONTINUE) |
| 4616 | goto done; |
| 4617 | } |
| 4618 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4619 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Paolo Bonzini | d40a689 | 2014-03-27 11:58:02 +0100 | [diff] [blame] | 4620 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
| 4621 | X86_ICPT_POST_EXCEPT); |
| 4622 | if (rc != X86EMUL_CONTINUE) |
| 4623 | goto done; |
| 4624 | } |
| 4625 | |
| 4626 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
| 4627 | /* All REP prefixes have the same first termination condition */ |
| 4628 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
| 4629 | ctxt->eip = ctxt->_eip; |
| 4630 | goto done; |
| 4631 | } |
| 4632 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4635 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4636 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4637 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4638 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4639 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4640 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4641 | } |
| 4642 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4643 | if (ctxt->src2.type == OP_MEM) { |
| 4644 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4645 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4646 | if (rc != X86EMUL_CONTINUE) |
| 4647 | goto done; |
| 4648 | } |
| 4649 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4650 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4651 | goto special_insn; |
| 4652 | |
| 4653 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4654 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4655 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4656 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4657 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4658 | if (rc != X86EMUL_CONTINUE) |
| 4659 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4660 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4661 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4662 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4663 | special_insn: |
| 4664 | |
Bandan Das | 685bbf4 | 2014-04-16 12:46:10 -0400 | [diff] [blame] | 4665 | if (unlikely(ctxt->guest_mode) && (ctxt->d & Intercept)) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4666 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4667 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4668 | if (rc != X86EMUL_CONTINUE) |
| 4669 | goto done; |
| 4670 | } |
| 4671 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4672 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4673 | if (ctxt->d & Fastop) { |
| 4674 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4675 | rc = fastop(ctxt, fop); |
| 4676 | if (rc != X86EMUL_CONTINUE) |
| 4677 | goto done; |
| 4678 | goto writeback; |
| 4679 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4680 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4681 | if (rc != X86EMUL_CONTINUE) |
| 4682 | goto done; |
| 4683 | goto writeback; |
| 4684 | } |
| 4685 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4686 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4687 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4688 | else if (ctxt->opcode_len == 3) |
| 4689 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4690 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4691 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4692 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4693 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4694 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4695 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4696 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4697 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4698 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4699 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4700 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4701 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4702 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4703 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4704 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4705 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Nadav Amit | a825f5c | 2014-06-15 16:13:01 +0300 | [diff] [blame] | 4706 | ctxt->dst.type = OP_NONE; |
| 4707 | else |
| 4708 | rc = em_xchg(ctxt); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4709 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4710 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4711 | switch (ctxt->op_bytes) { |
| 4712 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4713 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4714 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4715 | } |
| 4716 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4717 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4718 | rc = emulate_int(ctxt, 3); |
| 4719 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4720 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4721 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4722 | break; |
| 4723 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4724 | if (ctxt->eflags & EFLG_OF) |
| 4725 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4726 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4727 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4728 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4729 | jmp_rel(ctxt, ctxt->src.val); |
| 4730 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4731 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4732 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4733 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4734 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4735 | case 0xf5: /* cmc */ |
| 4736 | /* complement carry flag from eflags reg */ |
| 4737 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4738 | break; |
| 4739 | case 0xf8: /* clc */ |
| 4740 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4741 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4742 | case 0xf9: /* stc */ |
| 4743 | ctxt->eflags |= EFLG_CF; |
| 4744 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4745 | case 0xfc: /* cld */ |
| 4746 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4747 | break; |
| 4748 | case 0xfd: /* std */ |
| 4749 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4750 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4751 | default: |
| 4752 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4753 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4754 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4755 | if (rc != X86EMUL_CONTINUE) |
| 4756 | goto done; |
| 4757 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4758 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 4759 | if (ctxt->d & SrcWrite) { |
| 4760 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 4761 | rc = writeback(ctxt, &ctxt->src); |
| 4762 | if (rc != X86EMUL_CONTINUE) |
| 4763 | goto done; |
| 4764 | } |
Nadav Amit | ee21229 | 2014-06-15 16:12:58 +0300 | [diff] [blame] | 4765 | if (!(ctxt->d & NoWrite)) { |
| 4766 | rc = writeback(ctxt, &ctxt->dst); |
| 4767 | if (rc != X86EMUL_CONTINUE) |
| 4768 | goto done; |
| 4769 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4770 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4771 | /* |
| 4772 | * restore dst type in case the decoding will be reused |
| 4773 | * (happens for string instruction ) |
| 4774 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4775 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4776 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4777 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4778 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4779 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4780 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4781 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4782 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4783 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4784 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4785 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4786 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4787 | count = ctxt->src.count; |
| 4788 | else |
| 4789 | count = ctxt->dst.count; |
| 4790 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4791 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4792 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4793 | if (!string_insn_completed(ctxt)) { |
| 4794 | /* |
| 4795 | * Re-enter guest when pio read ahead buffer is empty |
| 4796 | * or, if it is not used, after each 1024 iteration. |
| 4797 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4798 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4799 | (r->end == 0 || r->end != r->pos)) { |
| 4800 | /* |
| 4801 | * Reset read cache. Usually happens before |
| 4802 | * decode, but since instruction is restarted |
| 4803 | * we have to do it here. |
| 4804 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4805 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4806 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4807 | return EMULATION_RESTART; |
| 4808 | } |
| 4809 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4810 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4811 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4812 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4813 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4814 | |
| 4815 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4816 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4817 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4818 | if (rc == X86EMUL_INTERCEPTED) |
| 4819 | return EMULATION_INTERCEPTED; |
| 4820 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4821 | if (rc == X86EMUL_CONTINUE) |
| 4822 | writeback_registers(ctxt); |
| 4823 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4824 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4825 | |
| 4826 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4827 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4828 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4829 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4830 | break; |
| 4831 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4832 | case 0x0d: /* GrpP (prefetch) */ |
| 4833 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4834 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4835 | break; |
| 4836 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4837 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4838 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4839 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4840 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4841 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4842 | case 0x40 ... 0x4f: /* cmov */ |
Nadav Amit | 140bad8 | 2014-06-15 16:13:00 +0300 | [diff] [blame] | 4843 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4844 | ctxt->dst.val = ctxt->src.val; |
| 4845 | else if (ctxt->mode != X86EMUL_MODE_PROT64 || |
| 4846 | ctxt->op_bytes != 4) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4847 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4848 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4849 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4850 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4851 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4852 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4853 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4854 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4855 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4856 | case 0xae: /* clflush */ |
| 4857 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4858 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4859 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4860 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4861 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4862 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4863 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4864 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4865 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4866 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4867 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4868 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4869 | ctxt->dst.bytes = ctxt->op_bytes; |
Nadav Amit | 3b32004 | 2014-06-02 18:34:08 +0300 | [diff] [blame] | 4870 | ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val : |
| 4871 | (u32) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4872 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4873 | default: |
| 4874 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4875 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4876 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4877 | threebyte_insn: |
| 4878 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4879 | if (rc != X86EMUL_CONTINUE) |
| 4880 | goto done; |
| 4881 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4882 | goto writeback; |
| 4883 | |
| 4884 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4885 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4886 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4887 | |
| 4888 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4889 | { |
| 4890 | invalidate_registers(ctxt); |
| 4891 | } |
| 4892 | |
| 4893 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4894 | { |
| 4895 | writeback_registers(ctxt); |
| 4896 | } |