Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 26 | #include <asm/kvm_emulate.h> |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 29 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 30 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | /* |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 33 | * Operand types |
| 34 | */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 35 | #define OpNone 0ull |
| 36 | #define OpImplicit 1ull /* No generic decode */ |
| 37 | #define OpReg 2ull /* Register */ |
| 38 | #define OpMem 3ull /* Memory */ |
| 39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ |
| 40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ |
| 41 | #define OpMem64 6ull /* Memory, 64-bit */ |
| 42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ |
| 43 | #define OpDX 8ull /* DX register */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 44 | #define OpCL 9ull /* CL register (for shifts) */ |
| 45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ |
| 46 | #define OpOne 11ull /* Implied 1 */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
| 49 | #define OpMem32 14ull /* Memory operand (32-bit). */ |
| 50 | #define OpImmU 15ull /* Immediate operand, zero extended */ |
| 51 | #define OpSI 16ull /* SI/ESI/RSI */ |
| 52 | #define OpImmFAddr 17ull /* Immediate far address */ |
| 53 | #define OpMemFAddr 18ull /* Far address in memory */ |
| 54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 55 | #define OpES 20ull /* ES */ |
| 56 | #define OpCS 21ull /* CS */ |
| 57 | #define OpSS 22ull /* SS */ |
| 58 | #define OpDS 23ull /* DS */ |
| 59 | #define OpFS 24ull /* FS */ |
| 60 | #define OpGS 25ull /* GS */ |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 63 | #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */ |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 64 | #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */ |
| 65 | #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 67 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 68 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 69 | |
| 70 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 71 | * Opcode effective-address decode tables. |
| 72 | * Note that we only emulate instructions that have at least one memory |
| 73 | * operand (excluding implicit stack references). We assume that stack |
| 74 | * references and instruction fetches will never occur in special memory |
| 75 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 76 | * not be handled. |
| 77 | */ |
| 78 | |
| 79 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 80 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 81 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 82 | #define DstShift 1 |
| 83 | #define ImplicitOps (OpImplicit << DstShift) |
| 84 | #define DstReg (OpReg << DstShift) |
| 85 | #define DstMem (OpMem << DstShift) |
| 86 | #define DstAcc (OpAcc << DstShift) |
| 87 | #define DstDI (OpDI << DstShift) |
| 88 | #define DstMem64 (OpMem64 << DstShift) |
| 89 | #define DstImmUByte (OpImmUByte << DstShift) |
| 90 | #define DstDX (OpDX << DstShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 91 | #define DstAccLo (OpAccLo << DstShift) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 92 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 94 | #define SrcShift 6 |
| 95 | #define SrcNone (OpNone << SrcShift) |
| 96 | #define SrcReg (OpReg << SrcShift) |
| 97 | #define SrcMem (OpMem << SrcShift) |
| 98 | #define SrcMem16 (OpMem16 << SrcShift) |
| 99 | #define SrcMem32 (OpMem32 << SrcShift) |
| 100 | #define SrcImm (OpImm << SrcShift) |
| 101 | #define SrcImmByte (OpImmByte << SrcShift) |
| 102 | #define SrcOne (OpOne << SrcShift) |
| 103 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 104 | #define SrcImmU (OpImmU << SrcShift) |
| 105 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 106 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 107 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 108 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 109 | #define SrcAcc (OpAcc << SrcShift) |
| 110 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 111 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 112 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 113 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 114 | #define SrcAccHi (OpAccHi << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 115 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 116 | #define BitOp (1<<11) |
| 117 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 118 | #define String (1<<13) /* String instruction (rep capable) */ |
| 119 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 120 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 121 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 122 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 123 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 124 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 125 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 126 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 127 | /* Generic ModRM decode. */ |
| 128 | #define ModRM (1<<19) |
| 129 | /* Destination is only written; never read. */ |
| 130 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 131 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 132 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 133 | #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 134 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 135 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 136 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 137 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 138 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 139 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 140 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 141 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 142 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 143 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 144 | #define Src2None (OpNone << Src2Shift) |
Avi Kivity | ab2c5ce | 2013-02-09 11:31:46 +0200 | [diff] [blame] | 145 | #define Src2Mem (OpMem << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 146 | #define Src2CL (OpCL << Src2Shift) |
| 147 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 148 | #define Src2One (OpOne << Src2Shift) |
| 149 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 150 | #define Src2ES (OpES << Src2Shift) |
| 151 | #define Src2CS (OpCS << Src2Shift) |
| 152 | #define Src2SS (OpSS << Src2Shift) |
| 153 | #define Src2DS (OpDS << Src2Shift) |
| 154 | #define Src2FS (OpFS << Src2Shift) |
| 155 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 156 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 157 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 158 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 159 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 160 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 161 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 162 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 163 | #define SrcWrite ((u64)1 << 46) /* Write back src operand */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 164 | #define NoMod ((u64)1 << 47) /* Mod field is ignored */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 165 | |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 166 | #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 167 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 168 | #define X2(x...) x, x |
| 169 | #define X3(x...) X2(x), x |
| 170 | #define X4(x...) X2(x), X2(x) |
| 171 | #define X5(x...) X4(x), x |
| 172 | #define X6(x...) X4(x), X2(x) |
| 173 | #define X7(x...) X4(x), X3(x) |
| 174 | #define X8(x...) X4(x), X4(x) |
| 175 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 176 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 177 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 178 | #define FASTOP_SIZE 8 |
| 179 | |
| 180 | /* |
| 181 | * fastop functions have a special calling convention: |
| 182 | * |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 183 | * dst: rax (in/out) |
| 184 | * src: rdx (in/out) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 185 | * src2: rcx (in) |
| 186 | * flags: rflags (in/out) |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 187 | * ex: rsi (in:fastop pointer, out:zero if exception) |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 188 | * |
| 189 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 190 | * different operand sizes can be reached by calculation, rather than a jump |
| 191 | * table (which would be bigger than the code). |
| 192 | * |
| 193 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 194 | * so they can't be called from C directly. |
| 195 | */ |
| 196 | |
| 197 | struct fastop; |
| 198 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 199 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 200 | u64 flags : 56; |
| 201 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 202 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 203 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 204 | const struct opcode *group; |
| 205 | const struct group_dual *gdual; |
| 206 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 207 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 208 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 209 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 210 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct group_dual { |
| 214 | struct opcode mod012[8]; |
| 215 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 216 | }; |
| 217 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 218 | struct gprefix { |
| 219 | struct opcode pfx_no; |
| 220 | struct opcode pfx_66; |
| 221 | struct opcode pfx_f2; |
| 222 | struct opcode pfx_f3; |
| 223 | }; |
| 224 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 225 | struct escape { |
| 226 | struct opcode op[8]; |
| 227 | struct opcode high[64]; |
| 228 | }; |
| 229 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 230 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 231 | #define EFLG_ID (1<<21) |
| 232 | #define EFLG_VIP (1<<20) |
| 233 | #define EFLG_VIF (1<<19) |
| 234 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 235 | #define EFLG_VM (1<<17) |
| 236 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 237 | #define EFLG_IOPL (3<<12) |
| 238 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 239 | #define EFLG_OF (1<<11) |
| 240 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 241 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 242 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 243 | #define EFLG_SF (1<<7) |
| 244 | #define EFLG_ZF (1<<6) |
| 245 | #define EFLG_AF (1<<4) |
| 246 | #define EFLG_PF (1<<2) |
| 247 | #define EFLG_CF (1<<0) |
| 248 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 249 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 250 | #define EFLG_RESERVED_ONE_MASK 2 |
| 251 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 252 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 253 | { |
| 254 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 255 | ctxt->regs_valid |= 1 << nr; |
| 256 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 257 | } |
| 258 | return ctxt->_regs[nr]; |
| 259 | } |
| 260 | |
| 261 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 262 | { |
| 263 | ctxt->regs_valid |= 1 << nr; |
| 264 | ctxt->regs_dirty |= 1 << nr; |
| 265 | return &ctxt->_regs[nr]; |
| 266 | } |
| 267 | |
| 268 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 269 | { |
| 270 | reg_read(ctxt, nr); |
| 271 | return reg_write(ctxt, nr); |
| 272 | } |
| 273 | |
| 274 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 275 | { |
| 276 | unsigned reg; |
| 277 | |
| 278 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 279 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 280 | } |
| 281 | |
| 282 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 283 | { |
| 284 | ctxt->regs_dirty = 0; |
| 285 | ctxt->regs_valid = 0; |
| 286 | } |
| 287 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 288 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 289 | * These EFLAGS bits are restored from saved value during emulation, and |
| 290 | * any changes are written back to the saved value after emulation. |
| 291 | */ |
| 292 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 293 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 294 | #ifdef CONFIG_X86_64 |
| 295 | #define ON64(x) x |
| 296 | #else |
| 297 | #define ON64(x) |
| 298 | #endif |
| 299 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 300 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 301 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 302 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 303 | #define FOP_RET "ret \n\t" |
| 304 | |
| 305 | #define FOP_START(op) \ |
| 306 | extern void em_##op(struct fastop *fake); \ |
| 307 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 308 | ".global em_" #op " \n\t" \ |
| 309 | FOP_ALIGN \ |
| 310 | "em_" #op ": \n\t" |
| 311 | |
| 312 | #define FOP_END \ |
| 313 | ".popsection") |
| 314 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 315 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 316 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 317 | #define FOP1E(op, dst) \ |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 318 | FOP_ALIGN "10: " #op " %" #dst " \n\t" FOP_RET |
| 319 | |
| 320 | #define FOP1EEX(op, dst) \ |
| 321 | FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception) |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 322 | |
| 323 | #define FASTOP1(op) \ |
| 324 | FOP_START(op) \ |
| 325 | FOP1E(op##b, al) \ |
| 326 | FOP1E(op##w, ax) \ |
| 327 | FOP1E(op##l, eax) \ |
| 328 | ON64(FOP1E(op##q, rax)) \ |
| 329 | FOP_END |
| 330 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 331 | /* 1-operand, using src2 (for MUL/DIV r/m) */ |
| 332 | #define FASTOP1SRC2(op, name) \ |
| 333 | FOP_START(name) \ |
| 334 | FOP1E(op, cl) \ |
| 335 | FOP1E(op, cx) \ |
| 336 | FOP1E(op, ecx) \ |
| 337 | ON64(FOP1E(op, rcx)) \ |
| 338 | FOP_END |
| 339 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 340 | /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */ |
| 341 | #define FASTOP1SRC2EX(op, name) \ |
| 342 | FOP_START(name) \ |
| 343 | FOP1EEX(op, cl) \ |
| 344 | FOP1EEX(op, cx) \ |
| 345 | FOP1EEX(op, ecx) \ |
| 346 | ON64(FOP1EEX(op, rcx)) \ |
| 347 | FOP_END |
| 348 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 349 | #define FOP2E(op, dst, src) \ |
| 350 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 351 | |
| 352 | #define FASTOP2(op) \ |
| 353 | FOP_START(op) \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 354 | FOP2E(op##b, al, dl) \ |
| 355 | FOP2E(op##w, ax, dx) \ |
| 356 | FOP2E(op##l, eax, edx) \ |
| 357 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 358 | FOP_END |
| 359 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 360 | /* 2 operand, word only */ |
| 361 | #define FASTOP2W(op) \ |
| 362 | FOP_START(op) \ |
| 363 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 364 | FOP2E(op##w, ax, dx) \ |
| 365 | FOP2E(op##l, eax, edx) \ |
| 366 | ON64(FOP2E(op##q, rax, rdx)) \ |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 367 | FOP_END |
| 368 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 369 | /* 2 operand, src is CL */ |
| 370 | #define FASTOP2CL(op) \ |
| 371 | FOP_START(op) \ |
| 372 | FOP2E(op##b, al, cl) \ |
| 373 | FOP2E(op##w, ax, cl) \ |
| 374 | FOP2E(op##l, eax, cl) \ |
| 375 | ON64(FOP2E(op##q, rax, cl)) \ |
| 376 | FOP_END |
| 377 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 378 | #define FOP3E(op, dst, src, src2) \ |
| 379 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 380 | |
| 381 | /* 3-operand, word-only, src2=cl */ |
| 382 | #define FASTOP3WCL(op) \ |
| 383 | FOP_START(op) \ |
| 384 | FOPNOP() \ |
Avi Kivity | 017da7b | 2013-02-09 11:31:47 +0200 | [diff] [blame] | 385 | FOP3E(op##w, ax, dx, cl) \ |
| 386 | FOP3E(op##l, eax, edx, cl) \ |
| 387 | ON64(FOP3E(op##q, rax, rdx, cl)) \ |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 388 | FOP_END |
| 389 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 390 | /* Special case for SETcc - 1 instruction per cc */ |
| 391 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 392 | |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 393 | asm(".global kvm_fastop_exception \n" |
| 394 | "kvm_fastop_exception: xor %esi, %esi; ret"); |
| 395 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 396 | FOP_START(setcc) |
| 397 | FOP_SETCC(seto) |
| 398 | FOP_SETCC(setno) |
| 399 | FOP_SETCC(setc) |
| 400 | FOP_SETCC(setnc) |
| 401 | FOP_SETCC(setz) |
| 402 | FOP_SETCC(setnz) |
| 403 | FOP_SETCC(setbe) |
| 404 | FOP_SETCC(setnbe) |
| 405 | FOP_SETCC(sets) |
| 406 | FOP_SETCC(setns) |
| 407 | FOP_SETCC(setp) |
| 408 | FOP_SETCC(setnp) |
| 409 | FOP_SETCC(setl) |
| 410 | FOP_SETCC(setnl) |
| 411 | FOP_SETCC(setle) |
| 412 | FOP_SETCC(setnle) |
| 413 | FOP_END; |
| 414 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 415 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 416 | FOP_END; |
| 417 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 418 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 419 | enum x86_intercept intercept, |
| 420 | enum x86_intercept_stage stage) |
| 421 | { |
| 422 | struct x86_instruction_info info = { |
| 423 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 424 | .rep_prefix = ctxt->rep_prefix, |
| 425 | .modrm_mod = ctxt->modrm_mod, |
| 426 | .modrm_reg = ctxt->modrm_reg, |
| 427 | .modrm_rm = ctxt->modrm_rm, |
| 428 | .src_val = ctxt->src.val64, |
| 429 | .src_bytes = ctxt->src.bytes, |
| 430 | .dst_bytes = ctxt->dst.bytes, |
| 431 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 432 | .next_rip = ctxt->eip, |
| 433 | }; |
| 434 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 435 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 438 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 439 | { |
| 440 | *dest = (*dest & ~mask) | (src & mask); |
| 441 | } |
| 442 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 443 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 444 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 445 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 448 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 449 | { |
| 450 | u16 sel; |
| 451 | struct desc_struct ss; |
| 452 | |
| 453 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 454 | return ~0UL; |
| 455 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 456 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 457 | } |
| 458 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 459 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 460 | { |
| 461 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 462 | } |
| 463 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 464 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 465 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 466 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 467 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 468 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 469 | return reg; |
| 470 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 471 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 475 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 476 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 477 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 480 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 481 | { |
| 482 | assign_masked(reg, *reg + inc, mask); |
| 483 | } |
| 484 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 485 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 486 | 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] | 487 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 488 | ulong mask; |
| 489 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 490 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 491 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 492 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 493 | mask = ad_mask(ctxt); |
| 494 | masked_increment(reg, mask, inc); |
| 495 | } |
| 496 | |
| 497 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 498 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 499 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 500 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 501 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 502 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 503 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 504 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 505 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 506 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 507 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 508 | { |
| 509 | u32 limit = get_desc_limit(desc); |
| 510 | |
| 511 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 512 | } |
| 513 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 514 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 515 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 516 | ctxt->has_seg_override = true; |
| 517 | ctxt->seg_override = seg; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 518 | } |
| 519 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 520 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 521 | { |
| 522 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 523 | return 0; |
| 524 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 525 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 526 | } |
| 527 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 528 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 529 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 530 | if (!ctxt->has_seg_override) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 531 | return 0; |
| 532 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 533 | return ctxt->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 534 | } |
| 535 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 536 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 537 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 538 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 539 | ctxt->exception.vector = vec; |
| 540 | ctxt->exception.error_code = error; |
| 541 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 542 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 543 | } |
| 544 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 545 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 546 | { |
| 547 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 548 | } |
| 549 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 550 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 551 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 552 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 555 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 556 | { |
| 557 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 558 | } |
| 559 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 560 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 561 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 562 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 563 | } |
| 564 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 565 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 566 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 567 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 568 | } |
| 569 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 570 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 571 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 572 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 573 | } |
| 574 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 575 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 576 | { |
| 577 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 578 | } |
| 579 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 580 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 581 | { |
| 582 | u16 selector; |
| 583 | struct desc_struct desc; |
| 584 | |
| 585 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 586 | return selector; |
| 587 | } |
| 588 | |
| 589 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 590 | unsigned seg) |
| 591 | { |
| 592 | u16 dummy; |
| 593 | u32 base3; |
| 594 | struct desc_struct desc; |
| 595 | |
| 596 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 597 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 598 | } |
| 599 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 600 | /* |
| 601 | * x86 defines three classes of vector instructions: explicitly |
| 602 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 603 | * depending on whether they're AVX encoded or not. |
| 604 | * |
| 605 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 606 | * subject to the same check. |
| 607 | */ |
| 608 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 609 | { |
| 610 | if (likely(size < 16)) |
| 611 | return false; |
| 612 | |
| 613 | if (ctxt->d & Aligned) |
| 614 | return true; |
| 615 | else if (ctxt->d & Unaligned) |
| 616 | return false; |
| 617 | else if (ctxt->d & Avx) |
| 618 | return false; |
| 619 | else |
| 620 | return true; |
| 621 | } |
| 622 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 623 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 624 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 625 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 626 | ulong *linear) |
| 627 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 628 | struct desc_struct desc; |
| 629 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 630 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 631 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 632 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 633 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 634 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 635 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 636 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 637 | case X86EMUL_MODE_PROT64: |
| 638 | if (((signed long)la << 16) >> 16 != la) |
| 639 | return emulate_gp(ctxt, 0); |
| 640 | break; |
| 641 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 642 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 643 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 644 | if (!usable) |
| 645 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 646 | /* code segment in protected mode or read-only data segment */ |
| 647 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 648 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 649 | goto bad; |
| 650 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 651 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 652 | goto bad; |
| 653 | lim = desc_limit_scaled(&desc); |
| 654 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 655 | /* expand-up segment */ |
| 656 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 657 | goto bad; |
| 658 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 659 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 660 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 661 | goto bad; |
| 662 | lim = desc.d ? 0xffffffff : 0xffff; |
| 663 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 664 | goto bad; |
| 665 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 666 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 667 | if (!(desc.type & 8)) { |
| 668 | /* data segment */ |
| 669 | if (cpl > desc.dpl) |
| 670 | goto bad; |
| 671 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 672 | /* nonconforming code segment */ |
| 673 | if (cpl != desc.dpl) |
| 674 | goto bad; |
| 675 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 676 | /* conforming code segment */ |
| 677 | if (cpl < desc.dpl) |
| 678 | goto bad; |
| 679 | } |
| 680 | break; |
| 681 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 682 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 683 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 684 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 685 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 686 | *linear = la; |
| 687 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 688 | bad: |
| 689 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 690 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 691 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 692 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 693 | } |
| 694 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 695 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 696 | struct segmented_address addr, |
| 697 | unsigned size, bool write, |
| 698 | ulong *linear) |
| 699 | { |
| 700 | return __linearize(ctxt, addr, size, write, false, linear); |
| 701 | } |
| 702 | |
| 703 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 704 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 705 | struct segmented_address addr, |
| 706 | void *data, |
| 707 | unsigned size) |
| 708 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 709 | int rc; |
| 710 | ulong linear; |
| 711 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 712 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 713 | if (rc != X86EMUL_CONTINUE) |
| 714 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 715 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 718 | /* |
| 719 | * Fetch the next byte of the instruction being emulated which is pointed to |
| 720 | * by ctxt->_eip, then increment ctxt->_eip. |
| 721 | * |
| 722 | * Also prefetch the remaining bytes of the instruction without crossing page |
| 723 | * boundary if they are not in fetch_cache yet. |
| 724 | */ |
| 725 | static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 726 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 727 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 728 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 729 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 730 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 731 | if (ctxt->_eip == fc->end) { |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 732 | unsigned long linear; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 733 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 734 | .ea = ctxt->_eip }; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 735 | cur_size = fc->end - fc->start; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 736 | size = min(15UL - cur_size, |
| 737 | PAGE_SIZE - offset_in_page(ctxt->_eip)); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 738 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 739 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 740 | return rc; |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 741 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 742 | size, &ctxt->exception); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 743 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 744 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 745 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 746 | } |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 747 | *dest = fc->data[ctxt->_eip - fc->start]; |
| 748 | ctxt->_eip++; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 749 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 753 | void *dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 754 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 755 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 756 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 757 | /* x86 instructions are limited to 15 bytes. */ |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 758 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 759 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 760 | while (size--) { |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 761 | rc = do_insn_fetch_byte(ctxt, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 762 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 763 | return rc; |
| 764 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 765 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 768 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 769 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 770 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 771 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 772 | if (rc != X86EMUL_CONTINUE) \ |
| 773 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 774 | (_type)_x; \ |
| 775 | }) |
| 776 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 777 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 778 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 779 | if (rc != X86EMUL_CONTINUE) \ |
| 780 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 781 | }) |
| 782 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 783 | /* |
| 784 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 785 | * pointer into the block that addresses the relevant register. |
| 786 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 787 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 788 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 789 | int byteop) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 790 | { |
| 791 | void *p; |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 792 | int highbyte_regs = (ctxt->rex_prefix == 0) && byteop; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 793 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 794 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 795 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 796 | else |
| 797 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 798 | return p; |
| 799 | } |
| 800 | |
| 801 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 802 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 803 | u16 *size, unsigned long *address, int op_bytes) |
| 804 | { |
| 805 | int rc; |
| 806 | |
| 807 | if (op_bytes == 2) |
| 808 | op_bytes = 3; |
| 809 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 810 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 811 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 812 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 813 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 814 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 815 | return rc; |
| 816 | } |
| 817 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 818 | FASTOP2(add); |
| 819 | FASTOP2(or); |
| 820 | FASTOP2(adc); |
| 821 | FASTOP2(sbb); |
| 822 | FASTOP2(and); |
| 823 | FASTOP2(sub); |
| 824 | FASTOP2(xor); |
| 825 | FASTOP2(cmp); |
| 826 | FASTOP2(test); |
| 827 | |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 828 | FASTOP1SRC2(mul, mul_ex); |
| 829 | FASTOP1SRC2(imul, imul_ex); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 830 | FASTOP1SRC2EX(div, div_ex); |
| 831 | FASTOP1SRC2EX(idiv, idiv_ex); |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 832 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 833 | FASTOP3WCL(shld); |
| 834 | FASTOP3WCL(shrd); |
| 835 | |
| 836 | FASTOP2W(imul); |
| 837 | |
| 838 | FASTOP1(not); |
| 839 | FASTOP1(neg); |
| 840 | FASTOP1(inc); |
| 841 | FASTOP1(dec); |
| 842 | |
| 843 | FASTOP2CL(rol); |
| 844 | FASTOP2CL(ror); |
| 845 | FASTOP2CL(rcl); |
| 846 | FASTOP2CL(rcr); |
| 847 | FASTOP2CL(shl); |
| 848 | FASTOP2CL(shr); |
| 849 | FASTOP2CL(sar); |
| 850 | |
| 851 | FASTOP2W(bsf); |
| 852 | FASTOP2W(bsr); |
| 853 | FASTOP2W(bt); |
| 854 | FASTOP2W(bts); |
| 855 | FASTOP2W(btr); |
| 856 | FASTOP2W(btc); |
| 857 | |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 858 | FASTOP2(xadd); |
| 859 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 860 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 861 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 862 | u8 rc; |
| 863 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 864 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 865 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 866 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 867 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 868 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 869 | } |
| 870 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 871 | static void fetch_register_operand(struct operand *op) |
| 872 | { |
| 873 | switch (op->bytes) { |
| 874 | case 1: |
| 875 | op->val = *(u8 *)op->addr.reg; |
| 876 | break; |
| 877 | case 2: |
| 878 | op->val = *(u16 *)op->addr.reg; |
| 879 | break; |
| 880 | case 4: |
| 881 | op->val = *(u32 *)op->addr.reg; |
| 882 | break; |
| 883 | case 8: |
| 884 | op->val = *(u64 *)op->addr.reg; |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 889 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 890 | { |
| 891 | ctxt->ops->get_fpu(ctxt); |
| 892 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 893 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 894 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 895 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 896 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 897 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 898 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 899 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 900 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 901 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 902 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 903 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 904 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 905 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 906 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 907 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 908 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 909 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 910 | #endif |
| 911 | default: BUG(); |
| 912 | } |
| 913 | ctxt->ops->put_fpu(ctxt); |
| 914 | } |
| 915 | |
| 916 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 917 | int reg) |
| 918 | { |
| 919 | ctxt->ops->get_fpu(ctxt); |
| 920 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 921 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 922 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 923 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 924 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 925 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 926 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 927 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 928 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 929 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 930 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 931 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 932 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 933 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 934 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 935 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 936 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 937 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 938 | #endif |
| 939 | default: BUG(); |
| 940 | } |
| 941 | ctxt->ops->put_fpu(ctxt); |
| 942 | } |
| 943 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 944 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 945 | { |
| 946 | ctxt->ops->get_fpu(ctxt); |
| 947 | switch (reg) { |
| 948 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 949 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 950 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 951 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 952 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 953 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 954 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 955 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 956 | default: BUG(); |
| 957 | } |
| 958 | ctxt->ops->put_fpu(ctxt); |
| 959 | } |
| 960 | |
| 961 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 962 | { |
| 963 | ctxt->ops->get_fpu(ctxt); |
| 964 | switch (reg) { |
| 965 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 966 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 967 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 968 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 969 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 970 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 971 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 972 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 973 | default: BUG(); |
| 974 | } |
| 975 | ctxt->ops->put_fpu(ctxt); |
| 976 | } |
| 977 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 978 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 979 | { |
| 980 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 981 | return emulate_nm(ctxt); |
| 982 | |
| 983 | ctxt->ops->get_fpu(ctxt); |
| 984 | asm volatile("fninit"); |
| 985 | ctxt->ops->put_fpu(ctxt); |
| 986 | return X86EMUL_CONTINUE; |
| 987 | } |
| 988 | |
| 989 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 990 | { |
| 991 | u16 fcw; |
| 992 | |
| 993 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 994 | return emulate_nm(ctxt); |
| 995 | |
| 996 | ctxt->ops->get_fpu(ctxt); |
| 997 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 998 | ctxt->ops->put_fpu(ctxt); |
| 999 | |
| 1000 | /* force 2 byte destination */ |
| 1001 | ctxt->dst.bytes = 2; |
| 1002 | ctxt->dst.val = fcw; |
| 1003 | |
| 1004 | return X86EMUL_CONTINUE; |
| 1005 | } |
| 1006 | |
| 1007 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1008 | { |
| 1009 | u16 fsw; |
| 1010 | |
| 1011 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1012 | return emulate_nm(ctxt); |
| 1013 | |
| 1014 | ctxt->ops->get_fpu(ctxt); |
| 1015 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1016 | ctxt->ops->put_fpu(ctxt); |
| 1017 | |
| 1018 | /* force 2 byte destination */ |
| 1019 | ctxt->dst.bytes = 2; |
| 1020 | ctxt->dst.val = fsw; |
| 1021 | |
| 1022 | return X86EMUL_CONTINUE; |
| 1023 | } |
| 1024 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1025 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1026 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1027 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1028 | unsigned reg = ctxt->modrm_reg; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1029 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1030 | if (!(ctxt->d & ModRM)) |
| 1031 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1032 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1033 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1034 | op->type = OP_XMM; |
| 1035 | op->bytes = 16; |
| 1036 | op->addr.xmm = reg; |
| 1037 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1038 | return; |
| 1039 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1040 | if (ctxt->d & Mmx) { |
| 1041 | reg &= 7; |
| 1042 | op->type = OP_MM; |
| 1043 | op->bytes = 8; |
| 1044 | op->addr.mm = reg; |
| 1045 | return; |
| 1046 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1047 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1048 | op->type = OP_REG; |
Gleb Natapov | 6d4d85e | 2013-11-04 15:52:42 +0200 | [diff] [blame] | 1049 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 1050 | op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp); |
| 1051 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1052 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1053 | op->orig_val = op->val; |
| 1054 | } |
| 1055 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1056 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1057 | { |
| 1058 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1059 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1060 | } |
| 1061 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1062 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1063 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1064 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1065 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1066 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1067 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1068 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1069 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1070 | if (ctxt->rex_prefix) { |
| 1071 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ |
| 1072 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ |
| 1073 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1076 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
| 1077 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
| 1078 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); |
| 1079 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1080 | |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 1081 | if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1082 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1083 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame] | 1084 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 1085 | ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1086 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1087 | op->type = OP_XMM; |
| 1088 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1089 | op->addr.xmm = ctxt->modrm_rm; |
| 1090 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1091 | return rc; |
| 1092 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1093 | if (ctxt->d & Mmx) { |
| 1094 | op->type = OP_MM; |
| 1095 | op->bytes = 8; |
| 1096 | op->addr.xmm = ctxt->modrm_rm & 7; |
| 1097 | return rc; |
| 1098 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1099 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1100 | return rc; |
| 1101 | } |
| 1102 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1103 | op->type = OP_MEM; |
| 1104 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1105 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1106 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1107 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1108 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1109 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1112 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1113 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1114 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1115 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1118 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1119 | break; |
| 1120 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1121 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1122 | break; |
| 1123 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1124 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1125 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1126 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1129 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1130 | break; |
| 1131 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1132 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1133 | break; |
| 1134 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1135 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1136 | break; |
| 1137 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1138 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1139 | break; |
| 1140 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1141 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1142 | break; |
| 1143 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1144 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1145 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1146 | break; |
| 1147 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1148 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1149 | break; |
| 1150 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1151 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1152 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1153 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1154 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1155 | } else { |
| 1156 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1157 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1158 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1159 | index_reg |= (sib >> 3) & 7; |
| 1160 | base_reg |= sib & 7; |
| 1161 | scale = sib >> 6; |
| 1162 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1163 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1164 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1165 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1166 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1167 | adjust_modrm_seg(ctxt, base_reg); |
| 1168 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1169 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1170 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1171 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1172 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1173 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1174 | } else { |
| 1175 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1176 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1177 | adjust_modrm_seg(ctxt, base_reg); |
| 1178 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1179 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1180 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1181 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1182 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1183 | break; |
| 1184 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1185 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1188 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1192 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1193 | done: |
| 1194 | return rc; |
| 1195 | } |
| 1196 | |
| 1197 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1198 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1199 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1200 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1201 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1202 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1203 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1204 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1205 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1206 | break; |
| 1207 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1208 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1209 | break; |
| 1210 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1211 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1212 | break; |
| 1213 | } |
| 1214 | done: |
| 1215 | return rc; |
| 1216 | } |
| 1217 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1218 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1219 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1220 | long sv = 0, 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 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
| 1223 | mask = ~(ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1224 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1225 | if (ctxt->src.bytes == 2) |
| 1226 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1227 | else if (ctxt->src.bytes == 4) |
| 1228 | sv = (s32)ctxt->src.val & (s32)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1229 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1230 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1231 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1232 | |
| 1233 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1234 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1237 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1238 | unsigned long addr, void *dest, unsigned size) |
| 1239 | { |
| 1240 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1241 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1242 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1243 | if (mc->pos < mc->end) |
| 1244 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1245 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1246 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1247 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1248 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1249 | &ctxt->exception); |
| 1250 | if (rc != X86EMUL_CONTINUE) |
| 1251 | return rc; |
| 1252 | |
| 1253 | mc->end += size; |
| 1254 | |
| 1255 | read_cached: |
| 1256 | memcpy(dest, mc->data + mc->pos, size); |
| 1257 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1258 | return X86EMUL_CONTINUE; |
| 1259 | } |
| 1260 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1261 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1262 | struct segmented_address addr, |
| 1263 | void *data, |
| 1264 | unsigned size) |
| 1265 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1266 | int rc; |
| 1267 | ulong linear; |
| 1268 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1269 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1270 | if (rc != X86EMUL_CONTINUE) |
| 1271 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1272 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1276 | struct segmented_address addr, |
| 1277 | const void *data, |
| 1278 | unsigned size) |
| 1279 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1280 | int rc; |
| 1281 | ulong linear; |
| 1282 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1283 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1284 | if (rc != X86EMUL_CONTINUE) |
| 1285 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1286 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1287 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1291 | struct segmented_address addr, |
| 1292 | const void *orig_data, const void *data, |
| 1293 | unsigned size) |
| 1294 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1295 | int rc; |
| 1296 | ulong linear; |
| 1297 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1298 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1299 | if (rc != X86EMUL_CONTINUE) |
| 1300 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1301 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1302 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1305 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1306 | unsigned int size, unsigned short port, |
| 1307 | void *dest) |
| 1308 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1309 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1310 | |
| 1311 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1312 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1313 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1314 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1315 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1316 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1317 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1318 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1319 | count); |
| 1320 | if (n == 0) |
| 1321 | n = 1; |
| 1322 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1323 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1324 | return 0; |
| 1325 | rc->end = n * size; |
| 1326 | } |
| 1327 | |
Nadav Amit | e6e39f0 | 2014-04-18 03:35:10 +0300 | [diff] [blame] | 1328 | if (ctxt->rep_prefix && (ctxt->d & String) && |
| 1329 | !(ctxt->eflags & EFLG_DF)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1330 | ctxt->dst.data = rc->data + rc->pos; |
| 1331 | ctxt->dst.type = OP_MEM_STR; |
| 1332 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1333 | rc->pos = rc->end; |
| 1334 | } else { |
| 1335 | memcpy(dest, rc->data + rc->pos, size); |
| 1336 | rc->pos += size; |
| 1337 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1338 | return 1; |
| 1339 | } |
| 1340 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1341 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1342 | u16 index, struct desc_struct *desc) |
| 1343 | { |
| 1344 | struct desc_ptr dt; |
| 1345 | ulong addr; |
| 1346 | |
| 1347 | ctxt->ops->get_idt(ctxt, &dt); |
| 1348 | |
| 1349 | if (dt.size < index * 8 + 7) |
| 1350 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1351 | |
| 1352 | addr = dt.address + index * 8; |
| 1353 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1354 | &ctxt->exception); |
| 1355 | } |
| 1356 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1357 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1358 | u16 selector, struct desc_ptr *dt) |
| 1359 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1360 | const struct x86_emulate_ops *ops = ctxt->ops; |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1361 | u32 base3 = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1362 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1363 | if (selector & 1 << 2) { |
| 1364 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1365 | u16 sel; |
| 1366 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1367 | memset (dt, 0, sizeof *dt); |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1368 | if (!ops->get_segment(ctxt, &sel, &desc, &base3, |
| 1369 | VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1370 | return; |
| 1371 | |
| 1372 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
Nadav Amit | 2eedcac | 2014-06-02 18:34:05 +0300 | [diff] [blame] | 1373 | dt->address = get_desc_base(&desc) | ((u64)base3 << 32); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1374 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1375 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* allowed just for 8 bytes segments */ |
| 1379 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1380 | u16 selector, struct desc_struct *desc, |
| 1381 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1382 | { |
| 1383 | struct desc_ptr dt; |
| 1384 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1385 | ulong addr; |
| 1386 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1387 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1388 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1389 | if (dt.size < index * 8 + 7) |
| 1390 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1391 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1392 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1393 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1394 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* allowed just for 8 bytes segments */ |
| 1398 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1399 | u16 selector, struct desc_struct *desc) |
| 1400 | { |
| 1401 | struct desc_ptr dt; |
| 1402 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1403 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1404 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1405 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1406 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1407 | if (dt.size < index * 8 + 7) |
| 1408 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1409 | |
| 1410 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1411 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1412 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1415 | /* Does not support long mode */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1416 | static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1417 | u16 selector, int seg, u8 cpl, bool in_task_switch) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1418 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1419 | struct desc_struct seg_desc, old_desc; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1420 | u8 dpl, rpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1421 | unsigned err_vec = GP_VECTOR; |
| 1422 | u32 err_code = 0; |
| 1423 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1424 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1425 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1426 | u16 dummy; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1427 | u32 base3 = 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1428 | |
| 1429 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1430 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1431 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1432 | /* set real mode segment descriptor (keep limit etc. for |
| 1433 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1434 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1435 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1436 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1437 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1438 | /* VM86 needs a clean new segment descriptor */ |
| 1439 | set_desc_base(&seg_desc, selector << 4); |
| 1440 | set_desc_limit(&seg_desc, 0xffff); |
| 1441 | seg_desc.type = 3; |
| 1442 | seg_desc.p = 1; |
| 1443 | seg_desc.s = 1; |
| 1444 | seg_desc.dpl = 3; |
| 1445 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1448 | rpl = selector & 3; |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1449 | |
| 1450 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1451 | if ((seg == VCPU_SREG_CS |
| 1452 | || (seg == VCPU_SREG_SS |
| 1453 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1454 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1455 | && null_selector) |
| 1456 | goto exception; |
| 1457 | |
| 1458 | /* TR should be in GDT only */ |
| 1459 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1460 | goto exception; |
| 1461 | |
| 1462 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1463 | goto load; |
| 1464 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1465 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1466 | if (ret != X86EMUL_CONTINUE) |
| 1467 | return ret; |
| 1468 | |
| 1469 | err_code = selector & 0xfffc; |
| 1470 | err_vec = GP_VECTOR; |
| 1471 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1472 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1473 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1474 | goto exception; |
| 1475 | |
| 1476 | if (!seg_desc.p) { |
| 1477 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1478 | goto exception; |
| 1479 | } |
| 1480 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1481 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1482 | |
| 1483 | switch (seg) { |
| 1484 | case VCPU_SREG_SS: |
| 1485 | /* |
| 1486 | * segment is not a writable data segment or segment |
| 1487 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1488 | */ |
| 1489 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1490 | goto exception; |
| 1491 | break; |
| 1492 | case VCPU_SREG_CS: |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1493 | if (in_task_switch && rpl != dpl) |
| 1494 | goto exception; |
| 1495 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1496 | if (!(seg_desc.type & 8)) |
| 1497 | goto exception; |
| 1498 | |
| 1499 | if (seg_desc.type & 4) { |
| 1500 | /* conforming */ |
| 1501 | if (dpl > cpl) |
| 1502 | goto exception; |
| 1503 | } else { |
| 1504 | /* nonconforming */ |
| 1505 | if (rpl > cpl || dpl != cpl) |
| 1506 | goto exception; |
| 1507 | } |
| 1508 | /* CS(RPL) <- CPL */ |
| 1509 | selector = (selector & 0xfffc) | cpl; |
| 1510 | break; |
| 1511 | case VCPU_SREG_TR: |
| 1512 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1513 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1514 | old_desc = seg_desc; |
| 1515 | seg_desc.type |= 2; /* busy */ |
| 1516 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1517 | sizeof(seg_desc), &ctxt->exception); |
| 1518 | if (ret != X86EMUL_CONTINUE) |
| 1519 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1520 | break; |
| 1521 | case VCPU_SREG_LDTR: |
| 1522 | if (seg_desc.s || seg_desc.type != 2) |
| 1523 | goto exception; |
| 1524 | break; |
| 1525 | default: /* DS, ES, FS, or GS */ |
| 1526 | /* |
| 1527 | * segment is not a data or readable code segment or |
| 1528 | * ((segment is a data or nonconforming code segment) |
| 1529 | * and (both RPL and CPL > DPL)) |
| 1530 | */ |
| 1531 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1532 | (((seg_desc.type & 0xc) != 0xc) && |
| 1533 | (rpl > dpl && cpl > dpl))) |
| 1534 | goto exception; |
| 1535 | break; |
| 1536 | } |
| 1537 | |
| 1538 | if (seg_desc.s) { |
| 1539 | /* mark segment as accessed */ |
| 1540 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1541 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1542 | if (ret != X86EMUL_CONTINUE) |
| 1543 | return ret; |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1544 | } else if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1545 | ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3, |
| 1546 | sizeof(base3), &ctxt->exception); |
| 1547 | if (ret != X86EMUL_CONTINUE) |
| 1548 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1549 | } |
| 1550 | load: |
Nadav Amit | e37a75a | 2014-06-02 18:34:04 +0300 | [diff] [blame] | 1551 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1552 | return X86EMUL_CONTINUE; |
| 1553 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1554 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1555 | return X86EMUL_PROPAGATE_FAULT; |
| 1556 | } |
| 1557 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1558 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1559 | u16 selector, int seg) |
| 1560 | { |
| 1561 | u8 cpl = ctxt->ops->cpl(ctxt); |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 1562 | return __load_segment_descriptor(ctxt, selector, seg, cpl, false); |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1565 | static void write_register_operand(struct operand *op) |
| 1566 | { |
| 1567 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1568 | switch (op->bytes) { |
| 1569 | case 1: |
| 1570 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1571 | break; |
| 1572 | case 2: |
| 1573 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1574 | break; |
| 1575 | case 4: |
| 1576 | *op->addr.reg = (u32)op->val; |
| 1577 | break; /* 64b: zero-extend */ |
| 1578 | case 8: |
| 1579 | *op->addr.reg = op->val; |
| 1580 | break; |
| 1581 | } |
| 1582 | } |
| 1583 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1584 | static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1585 | { |
| 1586 | int rc; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1587 | |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1588 | switch (op->type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1589 | case OP_REG: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1590 | write_register_operand(op); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1591 | break; |
| 1592 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1593 | if (ctxt->lock_prefix) |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1594 | rc = segmented_cmpxchg(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1595 | op->addr.mem, |
| 1596 | &op->orig_val, |
| 1597 | &op->val, |
| 1598 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1599 | else |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1600 | rc = segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1601 | op->addr.mem, |
| 1602 | &op->val, |
| 1603 | op->bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1604 | if (rc != X86EMUL_CONTINUE) |
| 1605 | return rc; |
| 1606 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1607 | case OP_MEM_STR: |
| 1608 | rc = segmented_write(ctxt, |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1609 | op->addr.mem, |
| 1610 | op->data, |
| 1611 | op->bytes * op->count); |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1612 | if (rc != X86EMUL_CONTINUE) |
| 1613 | return rc; |
| 1614 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1615 | case OP_XMM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1616 | write_sse_reg(ctxt, &op->vec_val, op->addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1617 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1618 | case OP_MM: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 1619 | write_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1620 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1621 | case OP_NONE: |
| 1622 | /* no writeback */ |
| 1623 | break; |
| 1624 | default: |
| 1625 | break; |
| 1626 | } |
| 1627 | return X86EMUL_CONTINUE; |
| 1628 | } |
| 1629 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1630 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1631 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1632 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1633 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1634 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1635 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1636 | addr.seg = VCPU_SREG_SS; |
| 1637 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1638 | return segmented_write(ctxt, addr, data, bytes); |
| 1639 | } |
| 1640 | |
| 1641 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1642 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1643 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1644 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1645 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1648 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1649 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1650 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1651 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1652 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1653 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1654 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1655 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1656 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1657 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1658 | return rc; |
| 1659 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1660 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1661 | return rc; |
| 1662 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1663 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1664 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1665 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1666 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1667 | } |
| 1668 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1669 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1670 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1671 | { |
| 1672 | int rc; |
| 1673 | unsigned long val, change_mask; |
| 1674 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1675 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1676 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1677 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1678 | if (rc != X86EMUL_CONTINUE) |
| 1679 | return rc; |
| 1680 | |
| 1681 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1682 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1683 | |
| 1684 | switch(ctxt->mode) { |
| 1685 | case X86EMUL_MODE_PROT64: |
| 1686 | case X86EMUL_MODE_PROT32: |
| 1687 | case X86EMUL_MODE_PROT16: |
| 1688 | if (cpl == 0) |
| 1689 | change_mask |= EFLG_IOPL; |
| 1690 | if (cpl <= iopl) |
| 1691 | change_mask |= EFLG_IF; |
| 1692 | break; |
| 1693 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1694 | if (iopl < 3) |
| 1695 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1696 | change_mask |= EFLG_IF; |
| 1697 | break; |
| 1698 | default: /* real mode */ |
| 1699 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | *(unsigned long *)dest = |
| 1704 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1705 | |
| 1706 | return rc; |
| 1707 | } |
| 1708 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1709 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1710 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1711 | ctxt->dst.type = OP_REG; |
| 1712 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1713 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1714 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1715 | } |
| 1716 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1717 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1718 | { |
| 1719 | int rc; |
| 1720 | unsigned frame_size = ctxt->src.val; |
| 1721 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1722 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1723 | |
| 1724 | if (nesting_level) |
| 1725 | return X86EMUL_UNHANDLEABLE; |
| 1726 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1727 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1728 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1729 | if (rc != X86EMUL_CONTINUE) |
| 1730 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1731 | 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] | 1732 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1733 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1734 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1735 | stack_mask(ctxt)); |
| 1736 | return X86EMUL_CONTINUE; |
| 1737 | } |
| 1738 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1739 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1740 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1741 | 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] | 1742 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1743 | 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] | 1744 | } |
| 1745 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1746 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1747 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1748 | int seg = ctxt->src2.val; |
| 1749 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1750 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1751 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1752 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1753 | } |
| 1754 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1755 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1756 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1757 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1758 | unsigned long selector; |
| 1759 | int rc; |
| 1760 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1761 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1762 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1763 | return rc; |
| 1764 | |
Paolo Bonzini | a5457e7 | 2014-06-05 17:29:34 +0200 | [diff] [blame^] | 1765 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
| 1766 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 1767 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1768 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1769 | return rc; |
| 1770 | } |
| 1771 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1772 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1773 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1774 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1775 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1776 | int reg = VCPU_REGS_RAX; |
| 1777 | |
| 1778 | while (reg <= VCPU_REGS_RDI) { |
| 1779 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1780 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1781 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1782 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1783 | if (rc != X86EMUL_CONTINUE) |
| 1784 | return rc; |
| 1785 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1786 | ++reg; |
| 1787 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1788 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1789 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1792 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1793 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1794 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1795 | return em_push(ctxt); |
| 1796 | } |
| 1797 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1798 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1799 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1800 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1801 | int reg = VCPU_REGS_RDI; |
| 1802 | |
| 1803 | while (reg >= VCPU_REGS_RAX) { |
| 1804 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1805 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1806 | --reg; |
| 1807 | } |
| 1808 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1809 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1810 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1811 | break; |
| 1812 | --reg; |
| 1813 | } |
| 1814 | return rc; |
| 1815 | } |
| 1816 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1817 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1818 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1819 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1820 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1821 | struct desc_ptr dt; |
| 1822 | gva_t cs_addr; |
| 1823 | gva_t eip_addr; |
| 1824 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1825 | |
| 1826 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1827 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1828 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1829 | if (rc != X86EMUL_CONTINUE) |
| 1830 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1831 | |
| 1832 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1833 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1834 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1835 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1836 | if (rc != X86EMUL_CONTINUE) |
| 1837 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1838 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1839 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1840 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1841 | if (rc != X86EMUL_CONTINUE) |
| 1842 | return rc; |
| 1843 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1844 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1845 | |
| 1846 | eip_addr = dt.address + (irq << 2); |
| 1847 | cs_addr = dt.address + (irq << 2) + 2; |
| 1848 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1849 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1850 | if (rc != X86EMUL_CONTINUE) |
| 1851 | return rc; |
| 1852 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1853 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1854 | if (rc != X86EMUL_CONTINUE) |
| 1855 | return rc; |
| 1856 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1857 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1858 | if (rc != X86EMUL_CONTINUE) |
| 1859 | return rc; |
| 1860 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1861 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1862 | |
| 1863 | return rc; |
| 1864 | } |
| 1865 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1866 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1867 | { |
| 1868 | int rc; |
| 1869 | |
| 1870 | invalidate_registers(ctxt); |
| 1871 | rc = __emulate_int_real(ctxt, irq); |
| 1872 | if (rc == X86EMUL_CONTINUE) |
| 1873 | writeback_registers(ctxt); |
| 1874 | return rc; |
| 1875 | } |
| 1876 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1877 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1878 | { |
| 1879 | switch(ctxt->mode) { |
| 1880 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1881 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1882 | case X86EMUL_MODE_VM86: |
| 1883 | case X86EMUL_MODE_PROT16: |
| 1884 | case X86EMUL_MODE_PROT32: |
| 1885 | case X86EMUL_MODE_PROT64: |
| 1886 | default: |
| 1887 | /* Protected mode interrupts unimplemented yet */ |
| 1888 | return X86EMUL_UNHANDLEABLE; |
| 1889 | } |
| 1890 | } |
| 1891 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1892 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1893 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1894 | int rc = X86EMUL_CONTINUE; |
| 1895 | unsigned long temp_eip = 0; |
| 1896 | unsigned long temp_eflags = 0; |
| 1897 | unsigned long cs = 0; |
| 1898 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1899 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1900 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1901 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1902 | |
| 1903 | /* TODO: Add stack limit check */ |
| 1904 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1905 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1906 | |
| 1907 | if (rc != X86EMUL_CONTINUE) |
| 1908 | return rc; |
| 1909 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1910 | if (temp_eip & ~0xffff) |
| 1911 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1912 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1913 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1914 | |
| 1915 | if (rc != X86EMUL_CONTINUE) |
| 1916 | return rc; |
| 1917 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1918 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1919 | |
| 1920 | if (rc != X86EMUL_CONTINUE) |
| 1921 | return rc; |
| 1922 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1923 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1924 | |
| 1925 | if (rc != X86EMUL_CONTINUE) |
| 1926 | return rc; |
| 1927 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1928 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1929 | |
| 1930 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1931 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1932 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1933 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1934 | ctxt->eflags &= ~0xffff; |
| 1935 | ctxt->eflags |= temp_eflags; |
| 1936 | } |
| 1937 | |
| 1938 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1939 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1940 | |
| 1941 | return rc; |
| 1942 | } |
| 1943 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 1944 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1945 | { |
| 1946 | switch(ctxt->mode) { |
| 1947 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1948 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1949 | case X86EMUL_MODE_VM86: |
| 1950 | case X86EMUL_MODE_PROT16: |
| 1951 | case X86EMUL_MODE_PROT32: |
| 1952 | case X86EMUL_MODE_PROT64: |
| 1953 | default: |
| 1954 | /* iret from protected mode unimplemented yet */ |
| 1955 | return X86EMUL_UNHANDLEABLE; |
| 1956 | } |
| 1957 | } |
| 1958 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1959 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 1960 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1961 | int rc; |
| 1962 | unsigned short sel; |
| 1963 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1964 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1965 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1966 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1967 | if (rc != X86EMUL_CONTINUE) |
| 1968 | return rc; |
| 1969 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1970 | ctxt->_eip = 0; |
| 1971 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1972 | return X86EMUL_CONTINUE; |
| 1973 | } |
| 1974 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 1975 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1976 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1977 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1978 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1979 | switch (ctxt->modrm_reg) { |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1980 | case 2: /* call near abs */ { |
| 1981 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1982 | old_eip = ctxt->_eip; |
| 1983 | ctxt->_eip = ctxt->src.val; |
| 1984 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1985 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1986 | break; |
| 1987 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1988 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1989 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1990 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 1991 | case 5: /* jmp far */ |
| 1992 | rc = em_jmp_far(ctxt); |
| 1993 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1994 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1995 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1996 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1997 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1998 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2001 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2002 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2003 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2004 | |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 2005 | if (ctxt->dst.bytes == 16) |
| 2006 | return X86EMUL_UNHANDLEABLE; |
| 2007 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2008 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2009 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2010 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2011 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2012 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2013 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2014 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2015 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2016 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2017 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2018 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2019 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2022 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2023 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2024 | ctxt->dst.type = OP_REG; |
| 2025 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2026 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2027 | return em_pop(ctxt); |
| 2028 | } |
| 2029 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2030 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2031 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2032 | int rc; |
| 2033 | unsigned long cs; |
| 2034 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2035 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2036 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2037 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2038 | if (ctxt->op_bytes == 4) |
| 2039 | ctxt->_eip = (u32)ctxt->_eip; |
| 2040 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2041 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2042 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2043 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2044 | return rc; |
| 2045 | } |
| 2046 | |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 2047 | static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt) |
| 2048 | { |
| 2049 | int rc; |
| 2050 | |
| 2051 | rc = em_ret_far(ctxt); |
| 2052 | if (rc != X86EMUL_CONTINUE) |
| 2053 | return rc; |
| 2054 | rsp_increment(ctxt, ctxt->src.val); |
| 2055 | return X86EMUL_CONTINUE; |
| 2056 | } |
| 2057 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2058 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2059 | { |
| 2060 | /* Save real source value, then compare EAX against destination. */ |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2061 | ctxt->dst.orig_val = ctxt->dst.val; |
| 2062 | ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2063 | ctxt->src.orig_val = ctxt->src.val; |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2064 | ctxt->src.val = ctxt->dst.orig_val; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2065 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2066 | |
| 2067 | if (ctxt->eflags & EFLG_ZF) { |
| 2068 | /* Success: write back to memory. */ |
| 2069 | ctxt->dst.val = ctxt->src.orig_val; |
| 2070 | } else { |
| 2071 | /* Failure: write the value we saw to EAX. */ |
| 2072 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2073 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Nadav Amit | 37c564f | 2014-06-02 18:34:07 +0300 | [diff] [blame] | 2074 | ctxt->dst.val = ctxt->dst.orig_val; |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2075 | } |
| 2076 | return X86EMUL_CONTINUE; |
| 2077 | } |
| 2078 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2079 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2080 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2081 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2082 | unsigned short sel; |
| 2083 | int rc; |
| 2084 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2085 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2086 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2087 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2088 | if (rc != X86EMUL_CONTINUE) |
| 2089 | return rc; |
| 2090 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2091 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2092 | return rc; |
| 2093 | } |
| 2094 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2095 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2096 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2097 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2098 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2099 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2100 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2101 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2102 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2103 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2104 | cs->s = 1; |
| 2105 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2106 | cs->p = 1; |
| 2107 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2108 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2109 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2110 | set_desc_base(ss, 0); /* flat segment */ |
| 2111 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2112 | ss->g = 1; /* 4kb granularity */ |
| 2113 | ss->s = 1; |
| 2114 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2115 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2116 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2117 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2118 | ss->l = 0; |
| 2119 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2120 | } |
| 2121 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2122 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2123 | { |
| 2124 | u32 eax, ebx, ecx, edx; |
| 2125 | |
| 2126 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2127 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2128 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2129 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2130 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2131 | } |
| 2132 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2133 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2134 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2135 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2136 | u32 eax, ebx, ecx, edx; |
| 2137 | |
| 2138 | /* |
| 2139 | * syscall should always be enabled in longmode - so only become |
| 2140 | * vendor specific (cpuid) if other modes are active... |
| 2141 | */ |
| 2142 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2143 | return true; |
| 2144 | |
| 2145 | eax = 0x00000000; |
| 2146 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2147 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2148 | /* |
| 2149 | * Intel ("GenuineIntel") |
| 2150 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2151 | * longmode. Also an 64bit guest with a |
| 2152 | * 32bit compat-app running will #UD !! While this |
| 2153 | * behaviour can be fixed (by emulating) into AMD |
| 2154 | * response - CPUs of AMD can't behave like Intel. |
| 2155 | */ |
| 2156 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2157 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2158 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2159 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2160 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2161 | /* AMD ("AuthenticAMD") */ |
| 2162 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2163 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2164 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2165 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2166 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2167 | /* AMD ("AMDisbetter!") */ |
| 2168 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2169 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2170 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2171 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2172 | |
| 2173 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2174 | return false; |
| 2175 | } |
| 2176 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2177 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2178 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2179 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2180 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2181 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2182 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2183 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2184 | |
| 2185 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2186 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2187 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2188 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2189 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2190 | if (!(em_syscall_is_enabled(ctxt))) |
| 2191 | return emulate_ud(ctxt); |
| 2192 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2193 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2194 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2195 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2196 | if (!(efer & EFER_SCE)) |
| 2197 | return emulate_ud(ctxt); |
| 2198 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2199 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2200 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2201 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2202 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2203 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2204 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2205 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2206 | cs.l = 1; |
| 2207 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2208 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2209 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2210 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2211 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2212 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2213 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2214 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2215 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2216 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2217 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2218 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2219 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2220 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2221 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2222 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2223 | #endif |
| 2224 | } else { |
| 2225 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2226 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2227 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2228 | |
| 2229 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2230 | } |
| 2231 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2232 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2233 | } |
| 2234 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2235 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2236 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2237 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2238 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2239 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2240 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2241 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2242 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2243 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2244 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2245 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2246 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2247 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2248 | /* |
| 2249 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2250 | * mode). |
| 2251 | */ |
| 2252 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2253 | && !vendor_intel(ctxt)) |
| 2254 | return emulate_ud(ctxt); |
| 2255 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2256 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2257 | * Therefore, we inject an #UD. |
| 2258 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2259 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2260 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2261 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2262 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2263 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2264 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2265 | switch (ctxt->mode) { |
| 2266 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2267 | if ((msr_data & 0xfffc) == 0x0) |
| 2268 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2269 | break; |
| 2270 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2271 | if (msr_data == 0x0) |
| 2272 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2273 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2274 | default: |
| 2275 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2279 | cs_sel = (u16)msr_data; |
| 2280 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2281 | ss_sel = cs_sel + 8; |
| 2282 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2283 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2284 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2285 | cs.l = 1; |
| 2286 | } |
| 2287 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2288 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2289 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2290 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2291 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2292 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2293 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2294 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2295 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2296 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2297 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2300 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2301 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2302 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2303 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2304 | u64 msr_data; |
| 2305 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2306 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2307 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2308 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2309 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2310 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2311 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2312 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2313 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2314 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2315 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2316 | usermode = X86EMUL_MODE_PROT64; |
| 2317 | else |
| 2318 | usermode = X86EMUL_MODE_PROT32; |
| 2319 | |
| 2320 | cs.dpl = 3; |
| 2321 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2322 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2323 | switch (usermode) { |
| 2324 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2325 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2326 | if ((msr_data & 0xfffc) == 0x0) |
| 2327 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2328 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2329 | break; |
| 2330 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2331 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2332 | if (msr_data == 0x0) |
| 2333 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2334 | ss_sel = cs_sel + 8; |
| 2335 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2336 | cs.l = 1; |
| 2337 | break; |
| 2338 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2339 | cs_sel |= SELECTOR_RPL_MASK; |
| 2340 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2341 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2342 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2343 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2344 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2345 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2346 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2347 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2348 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2351 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2352 | { |
| 2353 | int iopl; |
| 2354 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2355 | return false; |
| 2356 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2357 | return true; |
| 2358 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2359 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2363 | u16 port, u16 len) |
| 2364 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2365 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2366 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2367 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2368 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2369 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2370 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2371 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2372 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2373 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2374 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2375 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2376 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2377 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2378 | base = get_desc_base(&tr_seg); |
| 2379 | #ifdef CONFIG_X86_64 |
| 2380 | base |= ((u64)base3) << 32; |
| 2381 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2382 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2383 | if (r != X86EMUL_CONTINUE) |
| 2384 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2385 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2386 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2387 | 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] | 2388 | if (r != X86EMUL_CONTINUE) |
| 2389 | return false; |
| 2390 | if ((perm >> bit_idx) & mask) |
| 2391 | return false; |
| 2392 | return true; |
| 2393 | } |
| 2394 | |
| 2395 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2396 | u16 port, u16 len) |
| 2397 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2398 | if (ctxt->perm_ok) |
| 2399 | return true; |
| 2400 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2401 | if (emulator_bad_iopl(ctxt)) |
| 2402 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2403 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2404 | |
| 2405 | ctxt->perm_ok = true; |
| 2406 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2407 | return true; |
| 2408 | } |
| 2409 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2410 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2411 | struct tss_segment_16 *tss) |
| 2412 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2413 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2414 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2415 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2416 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2417 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2418 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2419 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2420 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2421 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2422 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2423 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2424 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2425 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2426 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2427 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2428 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2432 | struct tss_segment_16 *tss) |
| 2433 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2434 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2435 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2436 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2437 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2438 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2439 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2440 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2441 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2442 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2443 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2444 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2445 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2446 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2447 | |
| 2448 | /* |
| 2449 | * SDM says that segment selectors are loaded before segment |
| 2450 | * descriptors |
| 2451 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2452 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2453 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2454 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2455 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2456 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2457 | |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2458 | cpl = tss->cs & 3; |
| 2459 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2460 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2461 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2462 | * it is handled in a context of new task |
| 2463 | */ |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2464 | ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2465 | if (ret != X86EMUL_CONTINUE) |
| 2466 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2467 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2468 | if (ret != X86EMUL_CONTINUE) |
| 2469 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2470 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2471 | if (ret != X86EMUL_CONTINUE) |
| 2472 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2473 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2474 | if (ret != X86EMUL_CONTINUE) |
| 2475 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2476 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2477 | if (ret != X86EMUL_CONTINUE) |
| 2478 | return ret; |
| 2479 | |
| 2480 | return X86EMUL_CONTINUE; |
| 2481 | } |
| 2482 | |
| 2483 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2484 | u16 tss_selector, u16 old_tss_sel, |
| 2485 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2486 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2487 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2488 | struct tss_segment_16 tss_seg; |
| 2489 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2490 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2491 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2492 | 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] | 2493 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2494 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2495 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2496 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2497 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2498 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2499 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2500 | 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] | 2501 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2502 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2503 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2504 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2505 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2506 | 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] | 2507 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2508 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2509 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2510 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2511 | |
| 2512 | if (old_tss_sel != 0xffff) { |
| 2513 | tss_seg.prev_task_link = old_tss_sel; |
| 2514 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2515 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2516 | &tss_seg.prev_task_link, |
| 2517 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2518 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2519 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2520 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2521 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2524 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2528 | struct tss_segment_32 *tss) |
| 2529 | { |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2530 | /* CR3 and ldt selector are not saved intentionally */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2531 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2532 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2533 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2534 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2535 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2536 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2537 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2538 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2539 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2540 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2541 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2542 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2543 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2544 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2545 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2546 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2547 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2551 | struct tss_segment_32 *tss) |
| 2552 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2553 | int ret; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2554 | u8 cpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2555 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2556 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2557 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2558 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2559 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2562 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2563 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2564 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2565 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2566 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2567 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2568 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2569 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2570 | |
| 2571 | /* |
| 2572 | * SDM says that segment selectors are loaded before segment |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2573 | * descriptors. This is important because CPL checks will |
| 2574 | * use CS.RPL. |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2575 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2576 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2577 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2578 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2579 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2580 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2581 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2582 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2585 | * If we're switching between Protected Mode and VM86, we need to make |
| 2586 | * sure to update the mode before loading the segment descriptors so |
| 2587 | * that the selectors are interpreted correctly. |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2588 | */ |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2589 | if (ctxt->eflags & X86_EFLAGS_VM) { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2590 | ctxt->mode = X86EMUL_MODE_VM86; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2591 | cpl = 3; |
| 2592 | } else { |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2593 | ctxt->mode = X86EMUL_MODE_PROT32; |
Paolo Bonzini | 2356aae | 2014-05-15 17:56:57 +0200 | [diff] [blame] | 2594 | cpl = tss->cs & 3; |
| 2595 | } |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2596 | |
| 2597 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | * Now load segment descriptors. If fault happenes at this stage |
| 2599 | * it is handled in a context of new task |
| 2600 | */ |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2601 | 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] | 2602 | if (ret != X86EMUL_CONTINUE) |
| 2603 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2604 | ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2605 | if (ret != X86EMUL_CONTINUE) |
| 2606 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2607 | ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2608 | if (ret != X86EMUL_CONTINUE) |
| 2609 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2610 | ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2611 | if (ret != X86EMUL_CONTINUE) |
| 2612 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2613 | ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2614 | if (ret != X86EMUL_CONTINUE) |
| 2615 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2616 | ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2617 | if (ret != X86EMUL_CONTINUE) |
| 2618 | return ret; |
Paolo Bonzini | 5045b46 | 2014-05-15 18:09:29 +0200 | [diff] [blame] | 2619 | ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2620 | if (ret != X86EMUL_CONTINUE) |
| 2621 | return ret; |
| 2622 | |
| 2623 | return X86EMUL_CONTINUE; |
| 2624 | } |
| 2625 | |
| 2626 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2627 | u16 tss_selector, u16 old_tss_sel, |
| 2628 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2629 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2630 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | struct tss_segment_32 tss_seg; |
| 2632 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2633 | u32 new_tss_base = get_desc_base(new_desc); |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2634 | u32 eip_offset = offsetof(struct tss_segment_32, eip); |
| 2635 | u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2636 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2637 | 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] | 2638 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2639 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2640 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2641 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2642 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2643 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2644 | |
Nadav Amit | 5c7411e | 2014-04-07 18:37:47 +0300 | [diff] [blame] | 2645 | /* Only GP registers and segment selectors are saved */ |
| 2646 | ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip, |
| 2647 | ldt_sel_offset - eip_offset, &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2648 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2649 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2650 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2651 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2652 | 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] | 2653 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2654 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2655 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2656 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2657 | |
| 2658 | if (old_tss_sel != 0xffff) { |
| 2659 | tss_seg.prev_task_link = old_tss_sel; |
| 2660 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2661 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2662 | &tss_seg.prev_task_link, |
| 2663 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2664 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2665 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2667 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2668 | } |
| 2669 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2670 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2674 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2675 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2676 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2677 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2678 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2679 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2680 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2681 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2682 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2683 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2684 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2685 | |
| 2686 | /* FIXME: old_tss_base == ~0 ? */ |
| 2687 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2688 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2689 | if (ret != X86EMUL_CONTINUE) |
| 2690 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2691 | 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] | 2692 | if (ret != X86EMUL_CONTINUE) |
| 2693 | return ret; |
| 2694 | |
| 2695 | /* FIXME: check that next_tss_desc is tss */ |
| 2696 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2697 | /* |
| 2698 | * Check privileges. The three cases are task switch caused by... |
| 2699 | * |
| 2700 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2701 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2702 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2703 | */ |
| 2704 | if (reason == TASK_SWITCH_GATE) { |
| 2705 | if (idt_index != -1) { |
| 2706 | /* Software interrupts */ |
| 2707 | struct desc_struct task_gate_desc; |
| 2708 | int dpl; |
| 2709 | |
| 2710 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2711 | &task_gate_desc); |
| 2712 | if (ret != X86EMUL_CONTINUE) |
| 2713 | return ret; |
| 2714 | |
| 2715 | dpl = task_gate_desc.dpl; |
| 2716 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2717 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2718 | } |
| 2719 | } else if (reason != TASK_SWITCH_IRET) { |
| 2720 | int dpl = next_tss_desc.dpl; |
| 2721 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2722 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2725 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2726 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2727 | if (!next_tss_desc.p || |
| 2728 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2729 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2730 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2731 | return X86EMUL_PROPAGATE_FAULT; |
| 2732 | } |
| 2733 | |
| 2734 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2735 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2736 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | if (reason == TASK_SWITCH_IRET) |
| 2740 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2741 | |
| 2742 | /* 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] | 2743 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2744 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2745 | old_tss_sel = 0xffff; |
| 2746 | |
| 2747 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2748 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2749 | old_tss_base, &next_tss_desc); |
| 2750 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2751 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2752 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2753 | if (ret != X86EMUL_CONTINUE) |
| 2754 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2755 | |
| 2756 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2757 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2758 | |
| 2759 | if (reason != TASK_SWITCH_IRET) { |
| 2760 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2761 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2764 | 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] | 2765 | 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] | 2766 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2767 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2768 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2769 | ctxt->lock_prefix = 0; |
| 2770 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2771 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2774 | return ret; |
| 2775 | } |
| 2776 | |
| 2777 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2778 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2779 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2780 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2781 | int rc; |
| 2782 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2783 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2784 | ctxt->_eip = ctxt->eip; |
| 2785 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2786 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2787 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2788 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2789 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2790 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2791 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2792 | writeback_registers(ctxt); |
| 2793 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2794 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2795 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2798 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2799 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2800 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2801 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2802 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2803 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2804 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2807 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2808 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2809 | u8 al, old_al; |
| 2810 | bool af, cf, old_cf; |
| 2811 | |
| 2812 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2813 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2814 | |
| 2815 | old_al = al; |
| 2816 | old_cf = cf; |
| 2817 | cf = false; |
| 2818 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2819 | if ((al & 0x0f) > 9 || af) { |
| 2820 | al -= 6; |
| 2821 | cf = old_cf | (al >= 250); |
| 2822 | af = true; |
| 2823 | } else { |
| 2824 | af = false; |
| 2825 | } |
| 2826 | if (old_al > 0x99 || old_cf) { |
| 2827 | al -= 0x60; |
| 2828 | cf = true; |
| 2829 | } |
| 2830 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2831 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2832 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2833 | ctxt->src.type = OP_IMM; |
| 2834 | ctxt->src.val = 0; |
| 2835 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2836 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2837 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2838 | if (cf) |
| 2839 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2840 | if (af) |
| 2841 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2842 | return X86EMUL_CONTINUE; |
| 2843 | } |
| 2844 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 2845 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 2846 | { |
| 2847 | u8 al, ah; |
| 2848 | |
| 2849 | if (ctxt->src.val == 0) |
| 2850 | return emulate_de(ctxt); |
| 2851 | |
| 2852 | al = ctxt->dst.val & 0xff; |
| 2853 | ah = al / ctxt->src.val; |
| 2854 | al %= ctxt->src.val; |
| 2855 | |
| 2856 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 2857 | |
| 2858 | /* Set PF, ZF, SF */ |
| 2859 | ctxt->src.type = OP_IMM; |
| 2860 | ctxt->src.val = 0; |
| 2861 | ctxt->src.bytes = 1; |
| 2862 | fastop(ctxt, em_or); |
| 2863 | |
| 2864 | return X86EMUL_CONTINUE; |
| 2865 | } |
| 2866 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2867 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2868 | { |
| 2869 | u8 al = ctxt->dst.val & 0xff; |
| 2870 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2871 | |
| 2872 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2873 | |
| 2874 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2875 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 2876 | /* Set PF, ZF, SF */ |
| 2877 | ctxt->src.type = OP_IMM; |
| 2878 | ctxt->src.val = 0; |
| 2879 | ctxt->src.bytes = 1; |
| 2880 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2881 | |
| 2882 | return X86EMUL_CONTINUE; |
| 2883 | } |
| 2884 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 2885 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 2886 | { |
| 2887 | long rel = ctxt->src.val; |
| 2888 | |
| 2889 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 2890 | jmp_rel(ctxt, rel); |
| 2891 | return em_push(ctxt); |
| 2892 | } |
| 2893 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2894 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 2895 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2896 | u16 sel, old_cs; |
| 2897 | ulong old_eip; |
| 2898 | int rc; |
| 2899 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2900 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2901 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2902 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2903 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2904 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2905 | return X86EMUL_CONTINUE; |
| 2906 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2907 | ctxt->_eip = 0; |
| 2908 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2909 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2910 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2911 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2912 | if (rc != X86EMUL_CONTINUE) |
| 2913 | return rc; |
| 2914 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2915 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2916 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 2917 | } |
| 2918 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2919 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 2920 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2921 | int rc; |
| 2922 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2923 | ctxt->dst.type = OP_REG; |
| 2924 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2925 | ctxt->dst.bytes = ctxt->op_bytes; |
| 2926 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2927 | if (rc != X86EMUL_CONTINUE) |
| 2928 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 2929 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 2930 | return X86EMUL_CONTINUE; |
| 2931 | } |
| 2932 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2933 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 2934 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2935 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2936 | ctxt->src.val = ctxt->dst.val; |
| 2937 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2938 | |
| 2939 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2940 | ctxt->dst.val = ctxt->src.orig_val; |
| 2941 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 2942 | return X86EMUL_CONTINUE; |
| 2943 | } |
| 2944 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2945 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 2946 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2947 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 2948 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 2949 | } |
| 2950 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2951 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 2952 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2953 | ctxt->dst.type = OP_REG; |
| 2954 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2955 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2956 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 2957 | |
| 2958 | return X86EMUL_CONTINUE; |
| 2959 | } |
| 2960 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2961 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 2962 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2963 | u64 tsc = 0; |
| 2964 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2965 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2966 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 2967 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 2968 | return X86EMUL_CONTINUE; |
| 2969 | } |
| 2970 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2971 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 2972 | { |
| 2973 | u64 pmc; |
| 2974 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2975 | 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] | 2976 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2977 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 2978 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 2979 | return X86EMUL_CONTINUE; |
| 2980 | } |
| 2981 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2982 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 2983 | { |
Stefan Hajnoczi | 49597d8 | 2012-04-09 18:40:00 +0300 | [diff] [blame] | 2984 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 2985 | return X86EMUL_CONTINUE; |
| 2986 | } |
| 2987 | |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 2988 | #define FFL(x) bit(X86_FEATURE_##x) |
| 2989 | |
| 2990 | static int em_movbe(struct x86_emulate_ctxt *ctxt) |
| 2991 | { |
| 2992 | u32 ebx, ecx, edx, eax = 1; |
| 2993 | u16 tmp; |
| 2994 | |
| 2995 | /* |
| 2996 | * Check MOVBE is set in the guest-visible CPUID leaf. |
| 2997 | */ |
| 2998 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2999 | if (!(ecx & FFL(MOVBE))) |
| 3000 | return emulate_ud(ctxt); |
| 3001 | |
| 3002 | switch (ctxt->op_bytes) { |
| 3003 | case 2: |
| 3004 | /* |
| 3005 | * From MOVBE definition: "...When the operand size is 16 bits, |
| 3006 | * the upper word of the destination register remains unchanged |
| 3007 | * ..." |
| 3008 | * |
| 3009 | * Both casting ->valptr and ->val to u16 breaks strict aliasing |
| 3010 | * rules so we have to do the operation almost per hand. |
| 3011 | */ |
| 3012 | tmp = (u16)ctxt->src.val; |
| 3013 | ctxt->dst.val &= ~0xffffUL; |
| 3014 | ctxt->dst.val |= (unsigned long)swab16(tmp); |
| 3015 | break; |
| 3016 | case 4: |
| 3017 | ctxt->dst.val = swab32((u32)ctxt->src.val); |
| 3018 | break; |
| 3019 | case 8: |
| 3020 | ctxt->dst.val = swab64(ctxt->src.val); |
| 3021 | break; |
| 3022 | default: |
| 3023 | return X86EMUL_PROPAGATE_FAULT; |
| 3024 | } |
| 3025 | return X86EMUL_CONTINUE; |
| 3026 | } |
| 3027 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3028 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3029 | { |
| 3030 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3031 | return emulate_gp(ctxt, 0); |
| 3032 | |
| 3033 | /* Disable writeback. */ |
| 3034 | ctxt->dst.type = OP_NONE; |
| 3035 | return X86EMUL_CONTINUE; |
| 3036 | } |
| 3037 | |
| 3038 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3039 | { |
| 3040 | unsigned long val; |
| 3041 | |
| 3042 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3043 | val = ctxt->src.val & ~0ULL; |
| 3044 | else |
| 3045 | val = ctxt->src.val & ~0U; |
| 3046 | |
| 3047 | /* #UD condition is already handled. */ |
| 3048 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3049 | return emulate_gp(ctxt, 0); |
| 3050 | |
| 3051 | /* Disable writeback. */ |
| 3052 | ctxt->dst.type = OP_NONE; |
| 3053 | return X86EMUL_CONTINUE; |
| 3054 | } |
| 3055 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3056 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3057 | { |
| 3058 | u64 msr_data; |
| 3059 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3060 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3061 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3062 | 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] | 3063 | return emulate_gp(ctxt, 0); |
| 3064 | |
| 3065 | return X86EMUL_CONTINUE; |
| 3066 | } |
| 3067 | |
| 3068 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3069 | { |
| 3070 | u64 msr_data; |
| 3071 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3072 | 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] | 3073 | return emulate_gp(ctxt, 0); |
| 3074 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3075 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3076 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3077 | return X86EMUL_CONTINUE; |
| 3078 | } |
| 3079 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3080 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3081 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3082 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3083 | return emulate_ud(ctxt); |
| 3084 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3085 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3086 | return X86EMUL_CONTINUE; |
| 3087 | } |
| 3088 | |
| 3089 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3090 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3091 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3092 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3093 | 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] | 3094 | return emulate_ud(ctxt); |
| 3095 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3096 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3097 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3098 | |
| 3099 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3100 | ctxt->dst.type = OP_NONE; |
| 3101 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3102 | } |
| 3103 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3104 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3105 | { |
| 3106 | u16 sel = ctxt->src.val; |
| 3107 | |
| 3108 | /* Disable writeback. */ |
| 3109 | ctxt->dst.type = OP_NONE; |
| 3110 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3111 | } |
| 3112 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3113 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3114 | { |
| 3115 | u16 sel = ctxt->src.val; |
| 3116 | |
| 3117 | /* Disable writeback. */ |
| 3118 | ctxt->dst.type = OP_NONE; |
| 3119 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3120 | } |
| 3121 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3122 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3123 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3124 | int rc; |
| 3125 | ulong linear; |
| 3126 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3127 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3128 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3129 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3130 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3131 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3132 | return X86EMUL_CONTINUE; |
| 3133 | } |
| 3134 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3135 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3136 | { |
| 3137 | ulong cr0; |
| 3138 | |
| 3139 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3140 | cr0 &= ~X86_CR0_TS; |
| 3141 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3142 | return X86EMUL_CONTINUE; |
| 3143 | } |
| 3144 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3145 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3146 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3147 | int rc; |
| 3148 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3149 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3150 | return X86EMUL_UNHANDLEABLE; |
| 3151 | |
| 3152 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3153 | if (rc != X86EMUL_CONTINUE) |
| 3154 | return rc; |
| 3155 | |
| 3156 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3157 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3158 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3159 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3160 | return X86EMUL_CONTINUE; |
| 3161 | } |
| 3162 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3163 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3164 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3165 | struct desc_ptr *ptr)) |
| 3166 | { |
| 3167 | struct desc_ptr desc_ptr; |
| 3168 | |
| 3169 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3170 | ctxt->op_bytes = 8; |
| 3171 | get(ctxt, &desc_ptr); |
| 3172 | if (ctxt->op_bytes == 2) { |
| 3173 | ctxt->op_bytes = 4; |
| 3174 | desc_ptr.address &= 0x00ffffff; |
| 3175 | } |
| 3176 | /* Disable writeback. */ |
| 3177 | ctxt->dst.type = OP_NONE; |
| 3178 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3179 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3180 | } |
| 3181 | |
| 3182 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3183 | { |
| 3184 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3185 | } |
| 3186 | |
| 3187 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3188 | { |
| 3189 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3190 | } |
| 3191 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3192 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3193 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3194 | struct desc_ptr desc_ptr; |
| 3195 | int rc; |
| 3196 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3197 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3198 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3199 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3200 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3201 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3202 | if (rc != X86EMUL_CONTINUE) |
| 3203 | return rc; |
| 3204 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3205 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3206 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3207 | return X86EMUL_CONTINUE; |
| 3208 | } |
| 3209 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3210 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3211 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3212 | int rc; |
| 3213 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3214 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3215 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3216 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3217 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3218 | return rc; |
| 3219 | } |
| 3220 | |
| 3221 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3222 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3223 | struct desc_ptr desc_ptr; |
| 3224 | int rc; |
| 3225 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3226 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3227 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3228 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3229 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3230 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3231 | if (rc != X86EMUL_CONTINUE) |
| 3232 | return rc; |
| 3233 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3234 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3235 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3236 | return X86EMUL_CONTINUE; |
| 3237 | } |
| 3238 | |
| 3239 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3240 | { |
Nadav Amit | 32e94d0 | 2014-06-02 18:34:11 +0300 | [diff] [blame] | 3241 | if (ctxt->dst.type == OP_MEM) |
| 3242 | ctxt->dst.bytes = 2; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3243 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3244 | return X86EMUL_CONTINUE; |
| 3245 | } |
| 3246 | |
| 3247 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3248 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3249 | 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] | 3250 | | (ctxt->src.val & 0x0f)); |
| 3251 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3252 | return X86EMUL_CONTINUE; |
| 3253 | } |
| 3254 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3255 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3256 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3257 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3258 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3259 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3260 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3261 | |
| 3262 | return X86EMUL_CONTINUE; |
| 3263 | } |
| 3264 | |
| 3265 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3266 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3267 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3268 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3269 | |
| 3270 | return X86EMUL_CONTINUE; |
| 3271 | } |
| 3272 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3273 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3274 | { |
| 3275 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3276 | &ctxt->dst.val)) |
| 3277 | return X86EMUL_IO_NEEDED; |
| 3278 | |
| 3279 | return X86EMUL_CONTINUE; |
| 3280 | } |
| 3281 | |
| 3282 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3283 | { |
| 3284 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3285 | &ctxt->src.val, 1); |
| 3286 | /* Disable writeback. */ |
| 3287 | ctxt->dst.type = OP_NONE; |
| 3288 | return X86EMUL_CONTINUE; |
| 3289 | } |
| 3290 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3291 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3292 | { |
| 3293 | if (emulator_bad_iopl(ctxt)) |
| 3294 | return emulate_gp(ctxt, 0); |
| 3295 | |
| 3296 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3297 | return X86EMUL_CONTINUE; |
| 3298 | } |
| 3299 | |
| 3300 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3301 | { |
| 3302 | if (emulator_bad_iopl(ctxt)) |
| 3303 | return emulate_gp(ctxt, 0); |
| 3304 | |
| 3305 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3306 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3307 | return X86EMUL_CONTINUE; |
| 3308 | } |
| 3309 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3310 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3311 | { |
| 3312 | u32 eax, ebx, ecx, edx; |
| 3313 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3314 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3315 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3316 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3317 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3318 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3319 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3320 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3321 | return X86EMUL_CONTINUE; |
| 3322 | } |
| 3323 | |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3324 | static int em_sahf(struct x86_emulate_ctxt *ctxt) |
| 3325 | { |
| 3326 | u32 flags; |
| 3327 | |
| 3328 | flags = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF; |
| 3329 | flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8; |
| 3330 | |
| 3331 | ctxt->eflags &= ~0xffUL; |
| 3332 | ctxt->eflags |= flags | X86_EFLAGS_FIXED; |
| 3333 | return X86EMUL_CONTINUE; |
| 3334 | } |
| 3335 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3336 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3337 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3338 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3339 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3340 | return X86EMUL_CONTINUE; |
| 3341 | } |
| 3342 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3343 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3344 | { |
| 3345 | switch (ctxt->op_bytes) { |
| 3346 | #ifdef CONFIG_X86_64 |
| 3347 | case 8: |
| 3348 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3349 | break; |
| 3350 | #endif |
| 3351 | default: |
| 3352 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3353 | break; |
| 3354 | } |
| 3355 | return X86EMUL_CONTINUE; |
| 3356 | } |
| 3357 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3358 | static bool valid_cr(int nr) |
| 3359 | { |
| 3360 | switch (nr) { |
| 3361 | case 0: |
| 3362 | case 2 ... 4: |
| 3363 | case 8: |
| 3364 | return true; |
| 3365 | default: |
| 3366 | return false; |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3371 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3372 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3373 | return emulate_ud(ctxt); |
| 3374 | |
| 3375 | return X86EMUL_CONTINUE; |
| 3376 | } |
| 3377 | |
| 3378 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3379 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3380 | u64 new_val = ctxt->src.val64; |
| 3381 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3382 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3383 | |
| 3384 | static u64 cr_reserved_bits[] = { |
| 3385 | 0xffffffff00000000ULL, |
| 3386 | 0, 0, 0, /* CR3 checked later */ |
| 3387 | CR4_RESERVED_BITS, |
| 3388 | 0, 0, 0, |
| 3389 | CR8_RESERVED_BITS, |
| 3390 | }; |
| 3391 | |
| 3392 | if (!valid_cr(cr)) |
| 3393 | return emulate_ud(ctxt); |
| 3394 | |
| 3395 | if (new_val & cr_reserved_bits[cr]) |
| 3396 | return emulate_gp(ctxt, 0); |
| 3397 | |
| 3398 | switch (cr) { |
| 3399 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3400 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3401 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3402 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3403 | return emulate_gp(ctxt, 0); |
| 3404 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3405 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3406 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3407 | |
| 3408 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3409 | !(cr4 & X86_CR4_PAE)) |
| 3410 | return emulate_gp(ctxt, 0); |
| 3411 | |
| 3412 | break; |
| 3413 | } |
| 3414 | case 3: { |
| 3415 | u64 rsvd = 0; |
| 3416 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3417 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3418 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3419 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3420 | |
| 3421 | if (new_val & rsvd) |
| 3422 | return emulate_gp(ctxt, 0); |
| 3423 | |
| 3424 | break; |
| 3425 | } |
| 3426 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3427 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3428 | |
| 3429 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3430 | return emulate_gp(ctxt, 0); |
| 3431 | |
| 3432 | break; |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | return X86EMUL_CONTINUE; |
| 3437 | } |
| 3438 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3439 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3440 | { |
| 3441 | unsigned long dr7; |
| 3442 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3443 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3444 | |
| 3445 | /* Check if DR7.Global_Enable is set */ |
| 3446 | return dr7 & (1 << 13); |
| 3447 | } |
| 3448 | |
| 3449 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3450 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3451 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3452 | u64 cr4; |
| 3453 | |
| 3454 | if (dr > 7) |
| 3455 | return emulate_ud(ctxt); |
| 3456 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3457 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3458 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3459 | return emulate_ud(ctxt); |
| 3460 | |
| 3461 | if (check_dr7_gd(ctxt)) |
| 3462 | return emulate_db(ctxt); |
| 3463 | |
| 3464 | return X86EMUL_CONTINUE; |
| 3465 | } |
| 3466 | |
| 3467 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3468 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3469 | u64 new_val = ctxt->src.val64; |
| 3470 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3471 | |
| 3472 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3473 | return emulate_gp(ctxt, 0); |
| 3474 | |
| 3475 | return check_dr_read(ctxt); |
| 3476 | } |
| 3477 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3478 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3479 | { |
| 3480 | u64 efer; |
| 3481 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3482 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3483 | |
| 3484 | if (!(efer & EFER_SVME)) |
| 3485 | return emulate_ud(ctxt); |
| 3486 | |
| 3487 | return X86EMUL_CONTINUE; |
| 3488 | } |
| 3489 | |
| 3490 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3491 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3492 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3493 | |
| 3494 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3495 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3496 | return emulate_gp(ctxt, 0); |
| 3497 | |
| 3498 | return check_svme(ctxt); |
| 3499 | } |
| 3500 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3501 | static int check_rdtsc(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); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3504 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3505 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3506 | return emulate_ud(ctxt); |
| 3507 | |
| 3508 | return X86EMUL_CONTINUE; |
| 3509 | } |
| 3510 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3511 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3512 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3513 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3514 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3515 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3516 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 3517 | ctxt->ops->check_pmc(ctxt, rcx)) |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3518 | return emulate_gp(ctxt, 0); |
| 3519 | |
| 3520 | return X86EMUL_CONTINUE; |
| 3521 | } |
| 3522 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3523 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3524 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3525 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3526 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3527 | return emulate_gp(ctxt, 0); |
| 3528 | |
| 3529 | return X86EMUL_CONTINUE; |
| 3530 | } |
| 3531 | |
| 3532 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3533 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3534 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3535 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3536 | return emulate_gp(ctxt, 0); |
| 3537 | |
| 3538 | return X86EMUL_CONTINUE; |
| 3539 | } |
| 3540 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3541 | #define D(_y) { .flags = (_y) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3542 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3543 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
| 3544 | .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3545 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3546 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3547 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3548 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3549 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3550 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3551 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3552 | #define II(_f, _e, _i) \ |
| 3553 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3554 | #define IIP(_f, _e, _i, _p) \ |
| 3555 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ |
| 3556 | .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3557 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3558 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3559 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3560 | #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] | 3561 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3562 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3563 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3564 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3565 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3566 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3567 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3568 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3569 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3570 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3571 | DI(SrcNone | Priv, monitor), |
| 3572 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3573 | N, N, N, N, N, N, |
| 3574 | }; |
| 3575 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3576 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3577 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3578 | II(SrcNone | Prot | EmulateOnUD, em_vmmcall, vmmcall), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3579 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3580 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3581 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3582 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3583 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3584 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3585 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3586 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3587 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3588 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3589 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3590 | N, N, N, N, N, N, |
| 3591 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3592 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3593 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3594 | F(Lock, em_add), |
| 3595 | F(Lock | PageTable, em_or), |
| 3596 | F(Lock, em_adc), |
| 3597 | F(Lock, em_sbb), |
| 3598 | F(Lock | PageTable, em_and), |
| 3599 | F(Lock, em_sub), |
| 3600 | F(Lock, em_xor), |
| 3601 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3602 | }; |
| 3603 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3604 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3605 | 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] | 3606 | }; |
| 3607 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3608 | static const struct opcode group2[] = { |
| 3609 | F(DstMem | ModRM, em_rol), |
| 3610 | F(DstMem | ModRM, em_ror), |
| 3611 | F(DstMem | ModRM, em_rcl), |
| 3612 | F(DstMem | ModRM, em_rcr), |
| 3613 | F(DstMem | ModRM, em_shl), |
| 3614 | F(DstMem | ModRM, em_shr), |
| 3615 | F(DstMem | ModRM, em_shl), |
| 3616 | F(DstMem | ModRM, em_sar), |
| 3617 | }; |
| 3618 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3619 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3620 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3621 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3622 | F(DstMem | SrcNone | Lock, em_not), |
| 3623 | F(DstMem | SrcNone | Lock, em_neg), |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 3624 | F(DstXacc | Src2Mem, em_mul_ex), |
| 3625 | F(DstXacc | Src2Mem, em_imul_ex), |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 3626 | F(DstXacc | Src2Mem, em_div_ex), |
| 3627 | F(DstXacc | Src2Mem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3628 | }; |
| 3629 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3630 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3631 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3632 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3633 | N, N, N, N, N, N, |
| 3634 | }; |
| 3635 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3636 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3637 | F(DstMem | SrcNone | Lock, em_inc), |
| 3638 | F(DstMem | SrcNone | Lock, em_dec), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3639 | I(SrcMem | Stack, em_grp45), |
| 3640 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3641 | I(SrcMem | Stack, em_grp45), |
| 3642 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
Gleb Natapov | 188424b | 2013-04-11 12:32:14 +0300 | [diff] [blame] | 3643 | I(SrcMem | Stack, em_grp45), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3644 | }; |
| 3645 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3646 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3647 | DI(Prot, sldt), |
| 3648 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3649 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3650 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3651 | N, N, N, N, |
| 3652 | }; |
| 3653 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3654 | static const struct group_dual group7 = { { |
Nadav Amit | 606b1c3 | 2014-06-02 18:34:06 +0300 | [diff] [blame] | 3655 | II(Mov | DstMem, em_sgdt, sgdt), |
| 3656 | II(Mov | DstMem, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3657 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3658 | II(SrcMem | Priv, em_lidt, lidt), |
| 3659 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3660 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3661 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3662 | }, { |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3663 | I(SrcNone | Priv | EmulateOnUD, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3664 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3665 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3666 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3667 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3668 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3669 | } }; |
| 3670 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3671 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3672 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3673 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3674 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3675 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3676 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3677 | }; |
| 3678 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3679 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3680 | 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] | 3681 | }, { |
| 3682 | N, N, N, N, N, N, N, N, |
| 3683 | } }; |
| 3684 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3685 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3686 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3687 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3688 | }; |
| 3689 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3690 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3691 | 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] | 3692 | }; |
| 3693 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3694 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3695 | I(0, em_mov), N, N, N, |
| 3696 | }; |
| 3697 | |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3698 | static const struct gprefix pfx_0f_28_0f_29 = { |
Igor Mammedov | 6fec27d | 2014-03-15 21:02:00 +0100 | [diff] [blame] | 3699 | I(Aligned, em_mov), I(Aligned, em_mov), N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3700 | }; |
| 3701 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3702 | static const struct escape escape_d9 = { { |
| 3703 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3704 | }, { |
| 3705 | /* 0xC0 - 0xC7 */ |
| 3706 | N, N, N, N, N, N, N, N, |
| 3707 | /* 0xC8 - 0xCF */ |
| 3708 | N, N, N, N, N, N, N, N, |
| 3709 | /* 0xD0 - 0xC7 */ |
| 3710 | N, N, N, N, N, N, N, N, |
| 3711 | /* 0xD8 - 0xDF */ |
| 3712 | N, N, N, N, N, N, N, N, |
| 3713 | /* 0xE0 - 0xE7 */ |
| 3714 | N, N, N, N, N, N, N, N, |
| 3715 | /* 0xE8 - 0xEF */ |
| 3716 | N, N, N, N, N, N, N, N, |
| 3717 | /* 0xF0 - 0xF7 */ |
| 3718 | N, N, N, N, N, N, N, N, |
| 3719 | /* 0xF8 - 0xFF */ |
| 3720 | N, N, N, N, N, N, N, N, |
| 3721 | } }; |
| 3722 | |
| 3723 | static const struct escape escape_db = { { |
| 3724 | N, N, N, N, N, N, N, N, |
| 3725 | }, { |
| 3726 | /* 0xC0 - 0xC7 */ |
| 3727 | N, N, N, N, N, N, N, N, |
| 3728 | /* 0xC8 - 0xCF */ |
| 3729 | N, N, N, N, N, N, N, N, |
| 3730 | /* 0xD0 - 0xC7 */ |
| 3731 | N, N, N, N, N, N, N, N, |
| 3732 | /* 0xD8 - 0xDF */ |
| 3733 | N, N, N, N, N, N, N, N, |
| 3734 | /* 0xE0 - 0xE7 */ |
| 3735 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3736 | /* 0xE8 - 0xEF */ |
| 3737 | N, N, N, N, N, N, N, N, |
| 3738 | /* 0xF0 - 0xF7 */ |
| 3739 | N, N, N, N, N, N, N, N, |
| 3740 | /* 0xF8 - 0xFF */ |
| 3741 | N, N, N, N, N, N, N, N, |
| 3742 | } }; |
| 3743 | |
| 3744 | static const struct escape escape_dd = { { |
| 3745 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3746 | }, { |
| 3747 | /* 0xC0 - 0xC7 */ |
| 3748 | N, N, N, N, N, N, N, N, |
| 3749 | /* 0xC8 - 0xCF */ |
| 3750 | N, N, N, N, N, N, N, N, |
| 3751 | /* 0xD0 - 0xC7 */ |
| 3752 | N, N, N, N, N, N, N, N, |
| 3753 | /* 0xD8 - 0xDF */ |
| 3754 | N, N, N, N, N, N, N, N, |
| 3755 | /* 0xE0 - 0xE7 */ |
| 3756 | N, N, N, N, N, N, N, N, |
| 3757 | /* 0xE8 - 0xEF */ |
| 3758 | N, N, N, N, N, N, N, N, |
| 3759 | /* 0xF0 - 0xF7 */ |
| 3760 | N, N, N, N, N, N, N, N, |
| 3761 | /* 0xF8 - 0xFF */ |
| 3762 | N, N, N, N, N, N, N, N, |
| 3763 | } }; |
| 3764 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3765 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3766 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3767 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3768 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3769 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3770 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3771 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3772 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3773 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3774 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3775 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3776 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3777 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3778 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3779 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3780 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3781 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3782 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3783 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3784 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3785 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3786 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3787 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3788 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3789 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3790 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3791 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3792 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3793 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3794 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3795 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3796 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3797 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3798 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3799 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3800 | N, N, N, N, |
| 3801 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3802 | I(SrcImm | Mov | Stack, em_push), |
| 3803 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3804 | I(SrcImmByte | Mov | Stack, em_push), |
| 3805 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3806 | 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] | 3807 | 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] | 3808 | /* 0x70 - 0x7F */ |
| 3809 | X16(D(SrcImmByte)), |
| 3810 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3811 | G(ByteOp | DstMem | SrcImm, group1), |
| 3812 | G(DstMem | SrcImm, group1), |
| 3813 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3814 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3815 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3816 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3817 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3818 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3819 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3820 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3821 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3822 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3823 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3824 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3825 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3826 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3827 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3828 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3829 | II(ImplicitOps | Stack, em_pushf, pushf), |
Paolo Bonzini | 98f7363 | 2013-10-31 11:19:42 +0100 | [diff] [blame] | 3830 | II(ImplicitOps | Stack, em_popf, popf), |
| 3831 | I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3832 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3833 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3834 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3835 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3836 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3837 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3838 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3839 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3840 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3841 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3842 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3843 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3844 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3845 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3846 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3847 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3848 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3849 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3850 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3851 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3852 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3853 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3854 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
Bruce Rogers | 3261107 | 2013-09-09 09:40:20 -0600 | [diff] [blame] | 3855 | I(ImplicitOps | Stack | SrcImmU16, em_ret_far_imm), |
| 3856 | I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3857 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3858 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3859 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3860 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 3861 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3862 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 3863 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 3864 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 3865 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3866 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3867 | 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] | 3868 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3869 | X3(I(SrcImmByte, em_loop)), |
| 3870 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3871 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3872 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3873 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3874 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3875 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3876 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3877 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3878 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3879 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3880 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3881 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3882 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3883 | D(ImplicitOps), D(ImplicitOps), |
| 3884 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3885 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3886 | }; |
| 3887 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3888 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3889 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3890 | G(0, group6), GD(0, &group7), N, N, |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3891 | N, I(ImplicitOps | EmulateOnUD, em_syscall), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3892 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3893 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3894 | N, D(ImplicitOps | ModRM), N, N, |
| 3895 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 3896 | N, N, N, N, N, N, N, N, |
| 3897 | D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3898 | /* 0x20 - 0x2F */ |
Nadav Amit | 9b88ae9 | 2014-05-25 23:05:21 +0300 | [diff] [blame] | 3899 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read), |
| 3900 | DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read), |
| 3901 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write, |
| 3902 | check_cr_write), |
| 3903 | IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write, |
| 3904 | check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3905 | N, N, N, N, |
Igor Mammedov | 27ce825 | 2014-03-15 21:01:59 +0100 | [diff] [blame] | 3906 | GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29), |
| 3907 | GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29), |
| 3908 | N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3909 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3910 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3911 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3912 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3913 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3914 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 3915 | I(ImplicitOps | EmulateOnUD, em_sysenter), |
| 3916 | I(ImplicitOps | Priv | EmulateOnUD, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 3917 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3918 | N, N, N, N, N, N, N, N, |
| 3919 | /* 0x40 - 0x4F */ |
| 3920 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 3921 | /* 0x50 - 0x5F */ |
| 3922 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3923 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3924 | N, N, N, N, |
| 3925 | N, N, N, N, |
| 3926 | N, N, N, N, |
| 3927 | 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] | 3928 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3929 | N, N, N, N, |
| 3930 | N, N, N, N, |
| 3931 | N, N, N, N, |
| 3932 | 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] | 3933 | /* 0x80 - 0x8F */ |
| 3934 | X16(D(SrcImm)), |
| 3935 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 3936 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3937 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3938 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3939 | II(ImplicitOps, em_cpuid, cpuid), |
| 3940 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3941 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 3942 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3943 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3944 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3945 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3946 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3947 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 3948 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3949 | D(ModRM), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3950 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 3951 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3952 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3953 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3954 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 3955 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3956 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3957 | /* 0xB8 - 0xBF */ |
| 3958 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 3959 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3960 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 3961 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 3962 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3963 | /* 0xC0 - 0xC7 */ |
Avi Kivity | e47a5f5 | 2013-02-09 11:31:51 +0200 | [diff] [blame] | 3964 | F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd), |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 3965 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3966 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3967 | /* 0xC8 - 0xCF */ |
| 3968 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3969 | /* 0xD0 - 0xDF */ |
| 3970 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3971 | /* 0xE0 - 0xEF */ |
| 3972 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3973 | /* 0xF0 - 0xFF */ |
| 3974 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 3975 | }; |
| 3976 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3977 | static const struct gprefix three_byte_0f_38_f0 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3978 | I(DstReg | SrcMem | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3979 | }; |
| 3980 | |
| 3981 | static const struct gprefix three_byte_0f_38_f1 = { |
Borislav Petkov | 84cffe4 | 2013-10-29 12:54:56 +0100 | [diff] [blame] | 3982 | I(DstMem | SrcReg | Mov, em_movbe), N, N, N |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3983 | }; |
| 3984 | |
| 3985 | /* |
| 3986 | * Insns below are selected by the prefix which indexed by the third opcode |
| 3987 | * byte. |
| 3988 | */ |
| 3989 | static const struct opcode opcode_map_0f_38[256] = { |
| 3990 | /* 0x00 - 0x7f */ |
| 3991 | 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] | 3992 | /* 0x80 - 0xef */ |
| 3993 | X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), |
| 3994 | /* 0xf0 - 0xf1 */ |
| 3995 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f0), |
| 3996 | GP(EmulateOnUD | ModRM | Prefix, &three_byte_0f_38_f1), |
| 3997 | /* 0xf2 - 0xff */ |
| 3998 | N, N, X4(N), X8(N) |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 3999 | }; |
| 4000 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4001 | #undef D |
| 4002 | #undef N |
| 4003 | #undef G |
| 4004 | #undef GD |
| 4005 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4006 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4007 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4008 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4009 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4010 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4011 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4012 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4013 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4014 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4015 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4016 | { |
| 4017 | unsigned size; |
| 4018 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4019 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4020 | if (size == 8) |
| 4021 | size = 4; |
| 4022 | return size; |
| 4023 | } |
| 4024 | |
| 4025 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4026 | unsigned size, bool sign_extension) |
| 4027 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4028 | int rc = X86EMUL_CONTINUE; |
| 4029 | |
| 4030 | op->type = OP_IMM; |
| 4031 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4032 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4033 | /* NB. Immediates are sign-extended as necessary. */ |
| 4034 | switch (op->bytes) { |
| 4035 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4036 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4037 | break; |
| 4038 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4039 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4040 | break; |
| 4041 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4042 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4043 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4044 | case 8: |
| 4045 | op->val = insn_fetch(s64, ctxt); |
| 4046 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4047 | } |
| 4048 | if (!sign_extension) { |
| 4049 | switch (op->bytes) { |
| 4050 | case 1: |
| 4051 | op->val &= 0xff; |
| 4052 | break; |
| 4053 | case 2: |
| 4054 | op->val &= 0xffff; |
| 4055 | break; |
| 4056 | case 4: |
| 4057 | op->val &= 0xffffffff; |
| 4058 | break; |
| 4059 | } |
| 4060 | } |
| 4061 | done: |
| 4062 | return rc; |
| 4063 | } |
| 4064 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4065 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4066 | unsigned d) |
| 4067 | { |
| 4068 | int rc = X86EMUL_CONTINUE; |
| 4069 | |
| 4070 | switch (d) { |
| 4071 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4072 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4073 | break; |
| 4074 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4075 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4076 | break; |
| 4077 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4078 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4079 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4080 | *op = ctxt->memop; |
| 4081 | ctxt->memopp = op; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4082 | if ((ctxt->d & BitOp) && op == &ctxt->dst) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4083 | fetch_bit_operand(ctxt); |
| 4084 | op->orig_val = op->val; |
| 4085 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4086 | case OpMem64: |
Nadav Amit | aaa05f2 | 2014-06-02 18:34:10 +0300 | [diff] [blame] | 4087 | ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4088 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4089 | case OpAcc: |
| 4090 | op->type = OP_REG; |
| 4091 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4092 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4093 | fetch_register_operand(op); |
| 4094 | op->orig_val = op->val; |
| 4095 | break; |
Avi Kivity | 820207c | 2013-02-09 11:31:45 +0200 | [diff] [blame] | 4096 | case OpAccLo: |
| 4097 | op->type = OP_REG; |
| 4098 | op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes; |
| 4099 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
| 4100 | fetch_register_operand(op); |
| 4101 | op->orig_val = op->val; |
| 4102 | break; |
| 4103 | case OpAccHi: |
| 4104 | if (ctxt->d & ByteOp) { |
| 4105 | op->type = OP_NONE; |
| 4106 | break; |
| 4107 | } |
| 4108 | op->type = OP_REG; |
| 4109 | op->bytes = ctxt->op_bytes; |
| 4110 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
| 4111 | fetch_register_operand(op); |
| 4112 | op->orig_val = op->val; |
| 4113 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4114 | case OpDI: |
| 4115 | op->type = OP_MEM; |
| 4116 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4117 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4118 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4119 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4120 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4121 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4122 | break; |
| 4123 | case OpDX: |
| 4124 | op->type = OP_REG; |
| 4125 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4126 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4127 | fetch_register_operand(op); |
| 4128 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4129 | case OpCL: |
| 4130 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4131 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4132 | break; |
| 4133 | case OpImmByte: |
| 4134 | rc = decode_imm(ctxt, op, 1, true); |
| 4135 | break; |
| 4136 | case OpOne: |
| 4137 | op->bytes = 1; |
| 4138 | op->val = 1; |
| 4139 | break; |
| 4140 | case OpImm: |
| 4141 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4142 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4143 | case OpImm64: |
| 4144 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4145 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4146 | case OpMem8: |
| 4147 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4148 | if (ctxt->memop.type == OP_REG) { |
Gleb Natapov | aa9ac1a | 2013-11-04 15:52:41 +0200 | [diff] [blame] | 4149 | ctxt->memop.addr.reg = decode_register(ctxt, |
| 4150 | ctxt->modrm_rm, true); |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4151 | fetch_register_operand(&ctxt->memop); |
| 4152 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4153 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4154 | case OpMem16: |
| 4155 | ctxt->memop.bytes = 2; |
| 4156 | goto mem_common; |
| 4157 | case OpMem32: |
| 4158 | ctxt->memop.bytes = 4; |
| 4159 | goto mem_common; |
| 4160 | case OpImmU16: |
| 4161 | rc = decode_imm(ctxt, op, 2, false); |
| 4162 | break; |
| 4163 | case OpImmU: |
| 4164 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4165 | break; |
| 4166 | case OpSI: |
| 4167 | op->type = OP_MEM; |
| 4168 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4169 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4170 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4171 | op->addr.mem.seg = seg_override(ctxt); |
| 4172 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4173 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4174 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4175 | case OpXLat: |
| 4176 | op->type = OP_MEM; |
| 4177 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4178 | op->addr.mem.ea = |
| 4179 | register_address(ctxt, |
| 4180 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4181 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
| 4182 | op->addr.mem.seg = seg_override(ctxt); |
| 4183 | op->val = 0; |
| 4184 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4185 | case OpImmFAddr: |
| 4186 | op->type = OP_IMM; |
| 4187 | op->addr.mem.ea = ctxt->_eip; |
| 4188 | op->bytes = ctxt->op_bytes + 2; |
| 4189 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4190 | break; |
| 4191 | case OpMemFAddr: |
| 4192 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4193 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4194 | case OpES: |
| 4195 | op->val = VCPU_SREG_ES; |
| 4196 | break; |
| 4197 | case OpCS: |
| 4198 | op->val = VCPU_SREG_CS; |
| 4199 | break; |
| 4200 | case OpSS: |
| 4201 | op->val = VCPU_SREG_SS; |
| 4202 | break; |
| 4203 | case OpDS: |
| 4204 | op->val = VCPU_SREG_DS; |
| 4205 | break; |
| 4206 | case OpFS: |
| 4207 | op->val = VCPU_SREG_FS; |
| 4208 | break; |
| 4209 | case OpGS: |
| 4210 | op->val = VCPU_SREG_GS; |
| 4211 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4212 | case OpImplicit: |
| 4213 | /* Special instructions do their own operand decoding. */ |
| 4214 | default: |
| 4215 | op->type = OP_NONE; /* Disable writeback. */ |
| 4216 | break; |
| 4217 | } |
| 4218 | |
| 4219 | done: |
| 4220 | return rc; |
| 4221 | } |
| 4222 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4223 | 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] | 4224 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4225 | int rc = X86EMUL_CONTINUE; |
| 4226 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4227 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4228 | bool op_prefix = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4229 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4230 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4231 | ctxt->memop.type = OP_NONE; |
| 4232 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4233 | ctxt->_eip = ctxt->eip; |
| 4234 | ctxt->fetch.start = ctxt->_eip; |
| 4235 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4236 | ctxt->opcode_len = 1; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4237 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4238 | memcpy(ctxt->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4239 | |
| 4240 | switch (mode) { |
| 4241 | case X86EMUL_MODE_REAL: |
| 4242 | case X86EMUL_MODE_VM86: |
| 4243 | case X86EMUL_MODE_PROT16: |
| 4244 | def_op_bytes = def_ad_bytes = 2; |
| 4245 | break; |
| 4246 | case X86EMUL_MODE_PROT32: |
| 4247 | def_op_bytes = def_ad_bytes = 4; |
| 4248 | break; |
| 4249 | #ifdef CONFIG_X86_64 |
| 4250 | case X86EMUL_MODE_PROT64: |
| 4251 | def_op_bytes = 4; |
| 4252 | def_ad_bytes = 8; |
| 4253 | break; |
| 4254 | #endif |
| 4255 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4256 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4257 | } |
| 4258 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4259 | ctxt->op_bytes = def_op_bytes; |
| 4260 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4261 | |
| 4262 | /* Legacy prefixes. */ |
| 4263 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4264 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4265 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4266 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4267 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4268 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4269 | break; |
| 4270 | case 0x67: /* address-size override */ |
| 4271 | if (mode == X86EMUL_MODE_PROT64) |
| 4272 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4273 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4274 | else |
| 4275 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4276 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4277 | break; |
| 4278 | case 0x26: /* ES override */ |
| 4279 | case 0x2e: /* CS override */ |
| 4280 | case 0x36: /* SS override */ |
| 4281 | case 0x3e: /* DS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4282 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4283 | break; |
| 4284 | case 0x64: /* FS override */ |
| 4285 | case 0x65: /* GS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4286 | set_seg_override(ctxt, ctxt->b & 7); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4287 | break; |
| 4288 | case 0x40 ... 0x4f: /* REX */ |
| 4289 | if (mode != X86EMUL_MODE_PROT64) |
| 4290 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4291 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4292 | continue; |
| 4293 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4294 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4295 | break; |
| 4296 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4297 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4298 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4299 | break; |
| 4300 | default: |
| 4301 | goto done_prefixes; |
| 4302 | } |
| 4303 | |
| 4304 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4305 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4306 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | done_prefixes: |
| 4310 | |
| 4311 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4312 | if (ctxt->rex_prefix & 8) |
| 4313 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4314 | |
| 4315 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4316 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4317 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4318 | if (ctxt->b == 0x0f) { |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4319 | ctxt->opcode_len = 2; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4320 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4321 | opcode = twobyte_table[ctxt->b]; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4322 | |
| 4323 | /* 0F_38 opcode map */ |
| 4324 | if (ctxt->b == 0x38) { |
| 4325 | ctxt->opcode_len = 3; |
| 4326 | ctxt->b = insn_fetch(u8, ctxt); |
| 4327 | opcode = opcode_map_0f_38[ctxt->b]; |
| 4328 | } |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4329 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4330 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4331 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4332 | if (ctxt->d & ModRM) |
| 4333 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4334 | |
Nadav Amit | 7fe864d | 2014-06-02 18:34:03 +0300 | [diff] [blame] | 4335 | /* vex-prefix instructions are not implemented */ |
| 4336 | if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) && |
| 4337 | (mode == X86EMUL_MODE_PROT64 || |
| 4338 | (mode >= X86EMUL_MODE_PROT16 && (ctxt->modrm & 0x80)))) { |
| 4339 | ctxt->d = NotImpl; |
| 4340 | } |
| 4341 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4342 | while (ctxt->d & GroupMask) { |
| 4343 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4344 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4345 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4346 | opcode = opcode.u.group[goffset]; |
| 4347 | break; |
| 4348 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4349 | goffset = (ctxt->modrm >> 3) & 7; |
| 4350 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4351 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4352 | else |
| 4353 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4354 | break; |
| 4355 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4356 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4357 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4358 | break; |
| 4359 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4360 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4361 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4362 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4363 | switch (simd_prefix) { |
| 4364 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4365 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4366 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4367 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4368 | } |
| 4369 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4370 | case Escape: |
| 4371 | if (ctxt->modrm > 0xbf) |
| 4372 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4373 | else |
| 4374 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4375 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4376 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4377 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4378 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4379 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4380 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4381 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4382 | } |
| 4383 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4384 | ctxt->execute = opcode.u.execute; |
| 4385 | ctxt->check_perm = opcode.check_perm; |
| 4386 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4387 | |
| 4388 | /* Unrecognised? */ |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4389 | if (ctxt->d == 0 || (ctxt->d & NotImpl)) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4390 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4391 | |
Borislav Petkov | b51e974 | 2013-09-22 16:44:52 +0200 | [diff] [blame] | 4392 | if (!(ctxt->d & EmulateOnUD) && ctxt->ud) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4393 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4394 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4395 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
| 4396 | ctxt->op_bytes = 8; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4397 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4398 | if (ctxt->d & Op3264) { |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4399 | if (mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4400 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4401 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4402 | ctxt->op_bytes = 4; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4403 | } |
| 4404 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4405 | if (ctxt->d & Sse) |
| 4406 | ctxt->op_bytes = 16; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4407 | else if (ctxt->d & Mmx) |
| 4408 | ctxt->op_bytes = 8; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4409 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4410 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4411 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4412 | rc = decode_modrm(ctxt, &ctxt->memop); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4413 | if (!ctxt->has_seg_override) |
| 4414 | set_seg_override(ctxt, ctxt->modrm_seg); |
| 4415 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4416 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4417 | if (rc != X86EMUL_CONTINUE) |
| 4418 | goto done; |
| 4419 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4420 | if (!ctxt->has_seg_override) |
| 4421 | set_seg_override(ctxt, VCPU_SREG_DS); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4422 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4423 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4424 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4425 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
| 4426 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4427 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4428 | /* |
| 4429 | * Decode and fetch the source operand: register, memory |
| 4430 | * or immediate. |
| 4431 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4432 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4433 | if (rc != X86EMUL_CONTINUE) |
| 4434 | goto done; |
| 4435 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4436 | /* |
| 4437 | * Decode and fetch the second source operand: register, memory |
| 4438 | * or immediate. |
| 4439 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4440 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & 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 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4445 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4446 | |
| 4447 | done: |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4448 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
| 4449 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4450 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4451 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4452 | } |
| 4453 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4454 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4455 | { |
| 4456 | return ctxt->d & PageTable; |
| 4457 | } |
| 4458 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4459 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4460 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4461 | /* The second termination condition only applies for REPE |
| 4462 | * and REPNE. Test if the repeat string operation prefix is |
| 4463 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4464 | * corresponding termination condition according to: |
| 4465 | * - if REPE/REPZ and ZF = 0 then done |
| 4466 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4467 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4468 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4469 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4470 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4471 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4472 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4473 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4474 | return true; |
| 4475 | |
| 4476 | return false; |
| 4477 | } |
| 4478 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4479 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4480 | { |
| 4481 | bool fault = false; |
| 4482 | |
| 4483 | ctxt->ops->get_fpu(ctxt); |
| 4484 | asm volatile("1: fwait \n\t" |
| 4485 | "2: \n\t" |
| 4486 | ".pushsection .fixup,\"ax\" \n\t" |
| 4487 | "3: \n\t" |
| 4488 | "movb $1, %[fault] \n\t" |
| 4489 | "jmp 2b \n\t" |
| 4490 | ".popsection \n\t" |
| 4491 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4492 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4493 | ctxt->ops->put_fpu(ctxt); |
| 4494 | |
| 4495 | if (unlikely(fault)) |
| 4496 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4497 | |
| 4498 | return X86EMUL_CONTINUE; |
| 4499 | } |
| 4500 | |
| 4501 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4502 | struct operand *op) |
| 4503 | { |
| 4504 | if (op->type == OP_MM) |
| 4505 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4506 | } |
| 4507 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4508 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4509 | { |
| 4510 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | b9fa409 | 2013-02-09 11:31:48 +0200 | [diff] [blame] | 4511 | if (!(ctxt->d & ByteOp)) |
| 4512 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4513 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4514 | : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags), |
| 4515 | [fastop]"+S"(fop) |
| 4516 | : "c"(ctxt->src2.val)); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4517 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
Avi Kivity | b8c0b6a | 2013-02-09 11:31:49 +0200 | [diff] [blame] | 4518 | if (!fop) /* exception is returned in fop variable */ |
| 4519 | return emulate_de(ctxt); |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4520 | return X86EMUL_CONTINUE; |
| 4521 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4522 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4523 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4524 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4525 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4526 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4527 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4528 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4529 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4530 | |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4531 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4532 | (ctxt->d & Undefined)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4533 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 4534 | goto done; |
| 4535 | } |
| 4536 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4537 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4538 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4539 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4540 | goto done; |
| 4541 | } |
| 4542 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4543 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4544 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4545 | goto done; |
| 4546 | } |
| 4547 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4548 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4549 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4550 | rc = emulate_ud(ctxt); |
| 4551 | goto done; |
| 4552 | } |
| 4553 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4554 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4555 | rc = emulate_nm(ctxt); |
| 4556 | goto done; |
| 4557 | } |
| 4558 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4559 | if (ctxt->d & Mmx) { |
| 4560 | rc = flush_pending_x87_faults(ctxt); |
| 4561 | if (rc != X86EMUL_CONTINUE) |
| 4562 | goto done; |
| 4563 | /* |
| 4564 | * Now that we know the fpu is exception safe, we can fetch |
| 4565 | * operands from it. |
| 4566 | */ |
| 4567 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4568 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4569 | if (!(ctxt->d & Mov)) |
| 4570 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4571 | } |
| 4572 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4573 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4574 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4575 | X86_ICPT_PRE_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4576 | if (rc != X86EMUL_CONTINUE) |
| 4577 | goto done; |
| 4578 | } |
| 4579 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4580 | /* Privileged instruction can be executed only in CPL=0 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4581 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4582 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4583 | goto done; |
| 4584 | } |
| 4585 | |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4586 | /* Instruction can only be executed in protected mode */ |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 4587 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4588 | rc = emulate_ud(ctxt); |
| 4589 | goto done; |
| 4590 | } |
| 4591 | |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4592 | /* Do instruction specific permission checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4593 | if (ctxt->check_perm) { |
| 4594 | rc = ctxt->check_perm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4595 | if (rc != X86EMUL_CONTINUE) |
| 4596 | goto done; |
| 4597 | } |
| 4598 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4599 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4600 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4601 | X86_ICPT_POST_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4602 | if (rc != X86EMUL_CONTINUE) |
| 4603 | goto done; |
| 4604 | } |
| 4605 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4606 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4607 | /* All REP prefixes have the same first termination condition */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4608 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4609 | ctxt->eip = ctxt->_eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4610 | goto done; |
| 4611 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4612 | } |
| 4613 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4614 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4615 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4616 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4617 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4618 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4619 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4620 | } |
| 4621 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4622 | if (ctxt->src2.type == OP_MEM) { |
| 4623 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4624 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4625 | if (rc != X86EMUL_CONTINUE) |
| 4626 | goto done; |
| 4627 | } |
| 4628 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4629 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4630 | goto special_insn; |
| 4631 | |
| 4632 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4633 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4634 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4635 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4636 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4637 | if (rc != X86EMUL_CONTINUE) |
| 4638 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4639 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4640 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4641 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4642 | special_insn: |
| 4643 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4644 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4645 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4646 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4647 | if (rc != X86EMUL_CONTINUE) |
| 4648 | goto done; |
| 4649 | } |
| 4650 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4651 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4652 | if (ctxt->d & Fastop) { |
| 4653 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4654 | rc = fastop(ctxt, fop); |
| 4655 | if (rc != X86EMUL_CONTINUE) |
| 4656 | goto done; |
| 4657 | goto writeback; |
| 4658 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4659 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4660 | if (rc != X86EMUL_CONTINUE) |
| 4661 | goto done; |
| 4662 | goto writeback; |
| 4663 | } |
| 4664 | |
Borislav Petkov | 1ce19dc | 2013-09-22 16:44:51 +0200 | [diff] [blame] | 4665 | if (ctxt->opcode_len == 2) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4666 | goto twobyte_insn; |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4667 | else if (ctxt->opcode_len == 3) |
| 4668 | goto threebyte_insn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4670 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4671 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4672 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4673 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4674 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4675 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4676 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4677 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4678 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4679 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4680 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4681 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4682 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4683 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4684 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 4685 | break; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4686 | rc = em_xchg(ctxt); |
| 4687 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4688 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4689 | switch (ctxt->op_bytes) { |
| 4690 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4691 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4692 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4693 | } |
| 4694 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4695 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4696 | rc = emulate_int(ctxt, 3); |
| 4697 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4698 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4699 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4700 | break; |
| 4701 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4702 | if (ctxt->eflags & EFLG_OF) |
| 4703 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4704 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4705 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4706 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4707 | jmp_rel(ctxt, ctxt->src.val); |
| 4708 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4709 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4710 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4711 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4712 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4713 | case 0xf5: /* cmc */ |
| 4714 | /* complement carry flag from eflags reg */ |
| 4715 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4716 | break; |
| 4717 | case 0xf8: /* clc */ |
| 4718 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4719 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4720 | case 0xf9: /* stc */ |
| 4721 | ctxt->eflags |= EFLG_CF; |
| 4722 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4723 | case 0xfc: /* cld */ |
| 4724 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4725 | break; |
| 4726 | case 0xfd: /* std */ |
| 4727 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4728 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4729 | default: |
| 4730 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4731 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4732 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4733 | if (rc != X86EMUL_CONTINUE) |
| 4734 | goto done; |
| 4735 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4736 | writeback: |
Avi Kivity | fb32b1e | 2013-02-09 11:31:44 +0200 | [diff] [blame] | 4737 | if (!(ctxt->d & NoWrite)) { |
| 4738 | rc = writeback(ctxt, &ctxt->dst); |
| 4739 | if (rc != X86EMUL_CONTINUE) |
| 4740 | goto done; |
| 4741 | } |
| 4742 | if (ctxt->d & SrcWrite) { |
| 4743 | BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR); |
| 4744 | rc = writeback(ctxt, &ctxt->src); |
| 4745 | if (rc != X86EMUL_CONTINUE) |
| 4746 | goto done; |
| 4747 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4748 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4749 | /* |
| 4750 | * restore dst type in case the decoding will be reused |
| 4751 | * (happens for string instruction ) |
| 4752 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4753 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4754 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4755 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4756 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4757 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4758 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4759 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4760 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4761 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4762 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4763 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4764 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4765 | count = ctxt->src.count; |
| 4766 | else |
| 4767 | count = ctxt->dst.count; |
| 4768 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4769 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4770 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4771 | if (!string_insn_completed(ctxt)) { |
| 4772 | /* |
| 4773 | * Re-enter guest when pio read ahead buffer is empty |
| 4774 | * or, if it is not used, after each 1024 iteration. |
| 4775 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4776 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4777 | (r->end == 0 || r->end != r->pos)) { |
| 4778 | /* |
| 4779 | * Reset read cache. Usually happens before |
| 4780 | * decode, but since instruction is restarted |
| 4781 | * we have to do it here. |
| 4782 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4783 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4784 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4785 | return EMULATION_RESTART; |
| 4786 | } |
| 4787 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4788 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4789 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4790 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4791 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4792 | |
| 4793 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4794 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4795 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4796 | if (rc == X86EMUL_INTERCEPTED) |
| 4797 | return EMULATION_INTERCEPTED; |
| 4798 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4799 | if (rc == X86EMUL_CONTINUE) |
| 4800 | writeback_registers(ctxt); |
| 4801 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4802 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4803 | |
| 4804 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4805 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4806 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4807 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4808 | break; |
| 4809 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4810 | case 0x0d: /* GrpP (prefetch) */ |
| 4811 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4812 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4813 | break; |
| 4814 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4815 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4816 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4817 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4818 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4819 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4820 | case 0x40 ... 0x4f: /* cmov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4821 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
| 4822 | if (!test_cc(ctxt->b, ctxt->eflags)) |
| 4823 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4824 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4825 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4826 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4827 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4828 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4829 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4830 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4831 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4832 | case 0xae: /* clflush */ |
| 4833 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4834 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4835 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4836 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4837 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4838 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4839 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4840 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4841 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4842 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4843 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4844 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4845 | ctxt->dst.bytes = ctxt->op_bytes; |
Nadav Amit | 3b32004 | 2014-06-02 18:34:08 +0300 | [diff] [blame] | 4846 | ctxt->dst.val = (ctxt->op_bytes == 8) ? (u64) ctxt->src.val : |
| 4847 | (u32) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4848 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4849 | default: |
| 4850 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4851 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4852 | |
Borislav Petkov | 0bc5eed | 2013-10-29 12:54:10 +0100 | [diff] [blame] | 4853 | threebyte_insn: |
| 4854 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4855 | if (rc != X86EMUL_CONTINUE) |
| 4856 | goto done; |
| 4857 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4858 | goto writeback; |
| 4859 | |
| 4860 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4861 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4862 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4863 | |
| 4864 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4865 | { |
| 4866 | invalidate_registers(ctxt); |
| 4867 | } |
| 4868 | |
| 4869 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4870 | { |
| 4871 | writeback_registers(ctxt); |
| 4872 | } |