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 | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 64 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 65 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 66 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 67 | |
| 68 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 69 | * Opcode effective-address decode tables. |
| 70 | * Note that we only emulate instructions that have at least one memory |
| 71 | * operand (excluding implicit stack references). We assume that stack |
| 72 | * references and instruction fetches will never occur in special memory |
| 73 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 74 | * not be handled. |
| 75 | */ |
| 76 | |
| 77 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 78 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 79 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 80 | #define DstShift 1 |
| 81 | #define ImplicitOps (OpImplicit << DstShift) |
| 82 | #define DstReg (OpReg << DstShift) |
| 83 | #define DstMem (OpMem << DstShift) |
| 84 | #define DstAcc (OpAcc << DstShift) |
| 85 | #define DstDI (OpDI << DstShift) |
| 86 | #define DstMem64 (OpMem64 << DstShift) |
| 87 | #define DstImmUByte (OpImmUByte << DstShift) |
| 88 | #define DstDX (OpDX << DstShift) |
| 89 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 90 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 91 | #define SrcShift 6 |
| 92 | #define SrcNone (OpNone << SrcShift) |
| 93 | #define SrcReg (OpReg << SrcShift) |
| 94 | #define SrcMem (OpMem << SrcShift) |
| 95 | #define SrcMem16 (OpMem16 << SrcShift) |
| 96 | #define SrcMem32 (OpMem32 << SrcShift) |
| 97 | #define SrcImm (OpImm << SrcShift) |
| 98 | #define SrcImmByte (OpImmByte << SrcShift) |
| 99 | #define SrcOne (OpOne << SrcShift) |
| 100 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 101 | #define SrcImmU (OpImmU << SrcShift) |
| 102 | #define SrcSI (OpSI << SrcShift) |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 103 | #define SrcXLat (OpXLat << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 104 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 105 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 106 | #define SrcAcc (OpAcc << SrcShift) |
| 107 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 108 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 109 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 110 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 111 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 112 | #define BitOp (1<<11) |
| 113 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 114 | #define String (1<<13) /* String instruction (rep capable) */ |
| 115 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 116 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 117 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 118 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 119 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 120 | #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] | 121 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 122 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 123 | /* Generic ModRM decode. */ |
| 124 | #define ModRM (1<<19) |
| 125 | /* Destination is only written; never read. */ |
| 126 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 127 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 128 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 129 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 130 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 131 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 132 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 133 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 134 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 135 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 136 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 137 | #define NotImpl (1 << 30) /* instruction is not implemented */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 138 | /* Source 2 operand type */ |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 139 | #define Src2Shift (31) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 140 | #define Src2None (OpNone << Src2Shift) |
| 141 | #define Src2CL (OpCL << Src2Shift) |
| 142 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 143 | #define Src2One (OpOne << Src2Shift) |
| 144 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 145 | #define Src2ES (OpES << Src2Shift) |
| 146 | #define Src2CS (OpCS << Src2Shift) |
| 147 | #define Src2SS (OpSS << Src2Shift) |
| 148 | #define Src2DS (OpDS << Src2Shift) |
| 149 | #define Src2FS (OpFS << Src2Shift) |
| 150 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 151 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 152 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 153 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 154 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 155 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 156 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 157 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 158 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 159 | #define X2(x...) x, x |
| 160 | #define X3(x...) X2(x), x |
| 161 | #define X4(x...) X2(x), X2(x) |
| 162 | #define X5(x...) X4(x), x |
| 163 | #define X6(x...) X4(x), X2(x) |
| 164 | #define X7(x...) X4(x), X3(x) |
| 165 | #define X8(x...) X4(x), X4(x) |
| 166 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 167 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 168 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 169 | #define FASTOP_SIZE 8 |
| 170 | |
| 171 | /* |
| 172 | * fastop functions have a special calling convention: |
| 173 | * |
| 174 | * dst: [rdx]:rax (in/out) |
| 175 | * src: rbx (in/out) |
| 176 | * src2: rcx (in) |
| 177 | * flags: rflags (in/out) |
| 178 | * |
| 179 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 180 | * different operand sizes can be reached by calculation, rather than a jump |
| 181 | * table (which would be bigger than the code). |
| 182 | * |
| 183 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 184 | * so they can't be called from C directly. |
| 185 | */ |
| 186 | |
| 187 | struct fastop; |
| 188 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 189 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 190 | u64 flags : 56; |
| 191 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 192 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 193 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 194 | const struct opcode *group; |
| 195 | const struct group_dual *gdual; |
| 196 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 197 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 198 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 199 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 200 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | struct group_dual { |
| 204 | struct opcode mod012[8]; |
| 205 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 206 | }; |
| 207 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 208 | struct gprefix { |
| 209 | struct opcode pfx_no; |
| 210 | struct opcode pfx_66; |
| 211 | struct opcode pfx_f2; |
| 212 | struct opcode pfx_f3; |
| 213 | }; |
| 214 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 215 | struct escape { |
| 216 | struct opcode op[8]; |
| 217 | struct opcode high[64]; |
| 218 | }; |
| 219 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 221 | #define EFLG_ID (1<<21) |
| 222 | #define EFLG_VIP (1<<20) |
| 223 | #define EFLG_VIF (1<<19) |
| 224 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 225 | #define EFLG_VM (1<<17) |
| 226 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 227 | #define EFLG_IOPL (3<<12) |
| 228 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 229 | #define EFLG_OF (1<<11) |
| 230 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 231 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 232 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 233 | #define EFLG_SF (1<<7) |
| 234 | #define EFLG_ZF (1<<6) |
| 235 | #define EFLG_AF (1<<4) |
| 236 | #define EFLG_PF (1<<2) |
| 237 | #define EFLG_CF (1<<0) |
| 238 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 239 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 240 | #define EFLG_RESERVED_ONE_MASK 2 |
| 241 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 242 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 243 | { |
| 244 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 245 | ctxt->regs_valid |= 1 << nr; |
| 246 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 247 | } |
| 248 | return ctxt->_regs[nr]; |
| 249 | } |
| 250 | |
| 251 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 252 | { |
| 253 | ctxt->regs_valid |= 1 << nr; |
| 254 | ctxt->regs_dirty |= 1 << nr; |
| 255 | return &ctxt->_regs[nr]; |
| 256 | } |
| 257 | |
| 258 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 259 | { |
| 260 | reg_read(ctxt, nr); |
| 261 | return reg_write(ctxt, nr); |
| 262 | } |
| 263 | |
| 264 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 265 | { |
| 266 | unsigned reg; |
| 267 | |
| 268 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 269 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 270 | } |
| 271 | |
| 272 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 273 | { |
| 274 | ctxt->regs_dirty = 0; |
| 275 | ctxt->regs_valid = 0; |
| 276 | } |
| 277 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 278 | /* |
| 279 | * Instruction emulation: |
| 280 | * Most instructions are emulated directly via a fragment of inline assembly |
| 281 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 282 | * any modified flags. |
| 283 | */ |
| 284 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 285 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 286 | #define _LO32 "k" /* force 32-bit operand */ |
| 287 | #define _STK "%%rsp" /* stack pointer */ |
| 288 | #elif defined(__i386__) |
| 289 | #define _LO32 "" /* force 32-bit operand */ |
| 290 | #define _STK "%%esp" /* stack pointer */ |
| 291 | #endif |
| 292 | |
| 293 | /* |
| 294 | * These EFLAGS bits are restored from saved value during emulation, and |
| 295 | * any changes are written back to the saved value after emulation. |
| 296 | */ |
| 297 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 298 | |
| 299 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 300 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 301 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 302 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 303 | "push %"_tmp"; " \ |
| 304 | "push %"_tmp"; " \ |
| 305 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 306 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 307 | "pushf; " \ |
| 308 | "notl %"_LO32 _tmp"; " \ |
| 309 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 310 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 311 | "pop %"_tmp"; " \ |
| 312 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 313 | "popf; " \ |
| 314 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 315 | |
| 316 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 317 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 318 | /* _sav |= EFLAGS & _msk; */ \ |
| 319 | "pushf; " \ |
| 320 | "pop %"_tmp"; " \ |
| 321 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 322 | "orl %"_LO32 _tmp",%"_sav"; " |
| 323 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 324 | #ifdef CONFIG_X86_64 |
| 325 | #define ON64(x) x |
| 326 | #else |
| 327 | #define ON64(x) |
| 328 | #endif |
| 329 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 330 | #define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 331 | do { \ |
| 332 | __asm__ __volatile__ ( \ |
| 333 | _PRE_EFLAGS("0", "4", "2") \ |
| 334 | _op _suffix " %"_x"3,%1; " \ |
| 335 | _POST_EFLAGS("0", "4", "2") \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 336 | : "=m" ((ctxt)->eflags), \ |
| 337 | "+q" (*(_dsttype*)&(ctxt)->dst.val), \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 338 | "=&r" (_tmp) \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 339 | : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 340 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 341 | |
| 342 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 343 | /* Raw emulation: instruction has two explicit operands. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 344 | #define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 345 | do { \ |
| 346 | unsigned long _tmp; \ |
| 347 | \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 348 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 349 | case 2: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 350 | ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 351 | break; \ |
| 352 | case 4: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 353 | ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 354 | break; \ |
| 355 | case 8: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 356 | ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 357 | break; \ |
| 358 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 359 | } while (0) |
| 360 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 361 | #define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 362 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 363 | unsigned long _tmp; \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 364 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 365 | case 1: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 366 | ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 367 | break; \ |
| 368 | default: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 369 | __emulate_2op_nobyte(ctxt, _op, \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 370 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 371 | break; \ |
| 372 | } \ |
| 373 | } while (0) |
| 374 | |
| 375 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 376 | #define emulate_2op_SrcB(ctxt, _op) \ |
| 377 | __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 378 | |
| 379 | /* Source operand is byte, word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 380 | #define emulate_2op_SrcV(ctxt, _op) \ |
| 381 | __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 382 | |
| 383 | /* Source operand is word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 384 | #define emulate_2op_SrcV_nobyte(ctxt, _op) \ |
| 385 | __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 386 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 387 | /* Instruction has three operands and one operand is stored in ECX register */ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 388 | #define __emulate_2op_cl(ctxt, _op, _suffix, _type) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 389 | do { \ |
| 390 | unsigned long _tmp; \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 391 | _type _clv = (ctxt)->src2.val; \ |
| 392 | _type _srcv = (ctxt)->src.val; \ |
| 393 | _type _dstv = (ctxt)->dst.val; \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 394 | \ |
| 395 | __asm__ __volatile__ ( \ |
| 396 | _PRE_EFLAGS("0", "5", "2") \ |
| 397 | _op _suffix " %4,%1 \n" \ |
| 398 | _POST_EFLAGS("0", "5", "2") \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 399 | : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 400 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 401 | ); \ |
| 402 | \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 403 | (ctxt)->src2.val = (unsigned long) _clv; \ |
| 404 | (ctxt)->src2.val = (unsigned long) _srcv; \ |
| 405 | (ctxt)->dst.val = (unsigned long) _dstv; \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 406 | } while (0) |
| 407 | |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 408 | #define emulate_2op_cl(ctxt, _op) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 409 | do { \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 410 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 411 | case 2: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 412 | __emulate_2op_cl(ctxt, _op, "w", u16); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 413 | break; \ |
| 414 | case 4: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 415 | __emulate_2op_cl(ctxt, _op, "l", u32); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 416 | break; \ |
| 417 | case 8: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 418 | ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 419 | break; \ |
| 420 | } \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 421 | } while (0) |
| 422 | |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 423 | #define __emulate_1op(ctxt, _op, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 424 | do { \ |
| 425 | unsigned long _tmp; \ |
| 426 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 427 | __asm__ __volatile__ ( \ |
| 428 | _PRE_EFLAGS("0", "3", "2") \ |
| 429 | _op _suffix " %1; " \ |
| 430 | _POST_EFLAGS("0", "3", "2") \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 431 | : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 432 | "=&r" (_tmp) \ |
| 433 | : "i" (EFLAGS_MASK)); \ |
| 434 | } while (0) |
| 435 | |
| 436 | /* Instruction has only one explicit operand (no source operand). */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 437 | #define emulate_1op(ctxt, _op) \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 438 | do { \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 439 | switch ((ctxt)->dst.bytes) { \ |
| 440 | case 1: __emulate_1op(ctxt, _op, "b"); break; \ |
| 441 | case 2: __emulate_1op(ctxt, _op, "w"); break; \ |
| 442 | case 4: __emulate_1op(ctxt, _op, "l"); break; \ |
| 443 | case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 444 | } \ |
| 445 | } while (0) |
| 446 | |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 447 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)); |
| 448 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 449 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 450 | #define FOP_RET "ret \n\t" |
| 451 | |
| 452 | #define FOP_START(op) \ |
| 453 | extern void em_##op(struct fastop *fake); \ |
| 454 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 455 | ".global em_" #op " \n\t" \ |
| 456 | FOP_ALIGN \ |
| 457 | "em_" #op ": \n\t" |
| 458 | |
| 459 | #define FOP_END \ |
| 460 | ".popsection") |
| 461 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 462 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 463 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 464 | #define FOP1E(op, dst) \ |
| 465 | FOP_ALIGN #op " %" #dst " \n\t" FOP_RET |
| 466 | |
| 467 | #define FASTOP1(op) \ |
| 468 | FOP_START(op) \ |
| 469 | FOP1E(op##b, al) \ |
| 470 | FOP1E(op##w, ax) \ |
| 471 | FOP1E(op##l, eax) \ |
| 472 | ON64(FOP1E(op##q, rax)) \ |
| 473 | FOP_END |
| 474 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 475 | #define FOP2E(op, dst, src) \ |
| 476 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 477 | |
| 478 | #define FASTOP2(op) \ |
| 479 | FOP_START(op) \ |
| 480 | FOP2E(op##b, al, bl) \ |
| 481 | FOP2E(op##w, ax, bx) \ |
| 482 | FOP2E(op##l, eax, ebx) \ |
| 483 | ON64(FOP2E(op##q, rax, rbx)) \ |
| 484 | FOP_END |
| 485 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 486 | /* 2 operand, word only */ |
| 487 | #define FASTOP2W(op) \ |
| 488 | FOP_START(op) \ |
| 489 | FOPNOP() \ |
| 490 | FOP2E(op##w, ax, bx) \ |
| 491 | FOP2E(op##l, eax, ebx) \ |
| 492 | ON64(FOP2E(op##q, rax, rbx)) \ |
| 493 | FOP_END |
| 494 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 495 | /* 2 operand, src is CL */ |
| 496 | #define FASTOP2CL(op) \ |
| 497 | FOP_START(op) \ |
| 498 | FOP2E(op##b, al, cl) \ |
| 499 | FOP2E(op##w, ax, cl) \ |
| 500 | FOP2E(op##l, eax, cl) \ |
| 501 | ON64(FOP2E(op##q, rax, cl)) \ |
| 502 | FOP_END |
| 503 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 504 | #define FOP3E(op, dst, src, src2) \ |
| 505 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 506 | |
| 507 | /* 3-operand, word-only, src2=cl */ |
| 508 | #define FASTOP3WCL(op) \ |
| 509 | FOP_START(op) \ |
| 510 | FOPNOP() \ |
| 511 | FOP3E(op##w, ax, bx, cl) \ |
| 512 | FOP3E(op##l, eax, ebx, cl) \ |
| 513 | ON64(FOP3E(op##q, rax, rbx, cl)) \ |
| 514 | FOP_END |
| 515 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 516 | /* Special case for SETcc - 1 instruction per cc */ |
| 517 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 518 | |
| 519 | FOP_START(setcc) |
| 520 | FOP_SETCC(seto) |
| 521 | FOP_SETCC(setno) |
| 522 | FOP_SETCC(setc) |
| 523 | FOP_SETCC(setnc) |
| 524 | FOP_SETCC(setz) |
| 525 | FOP_SETCC(setnz) |
| 526 | FOP_SETCC(setbe) |
| 527 | FOP_SETCC(setnbe) |
| 528 | FOP_SETCC(sets) |
| 529 | FOP_SETCC(setns) |
| 530 | FOP_SETCC(setp) |
| 531 | FOP_SETCC(setnp) |
| 532 | FOP_SETCC(setl) |
| 533 | FOP_SETCC(setnl) |
| 534 | FOP_SETCC(setle) |
| 535 | FOP_SETCC(setnle) |
| 536 | FOP_END; |
| 537 | |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 538 | FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET |
| 539 | FOP_END; |
| 540 | |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 541 | #define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 542 | do { \ |
| 543 | unsigned long _tmp; \ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 544 | ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \ |
| 545 | ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 546 | \ |
| 547 | __asm__ __volatile__ ( \ |
| 548 | _PRE_EFLAGS("0", "5", "1") \ |
| 549 | "1: \n\t" \ |
| 550 | _op _suffix " %6; " \ |
| 551 | "2: \n\t" \ |
| 552 | _POST_EFLAGS("0", "5", "1") \ |
| 553 | ".pushsection .fixup,\"ax\" \n\t" \ |
| 554 | "3: movb $1, %4 \n\t" \ |
| 555 | "jmp 2b \n\t" \ |
| 556 | ".popsection \n\t" \ |
| 557 | _ASM_EXTABLE(1b, 3b) \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 558 | : "=m" ((ctxt)->eflags), "=&r" (_tmp), \ |
| 559 | "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \ |
H. Peter Anvin | cb7cb28 | 2012-11-21 14:41:21 -0800 | [diff] [blame] | 560 | : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 561 | } while (0) |
| 562 | |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 563 | /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 564 | #define emulate_1op_rax_rdx(ctxt, _op, _ex) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 565 | do { \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 566 | switch((ctxt)->src.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 567 | case 1: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 568 | __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 569 | break; \ |
| 570 | case 2: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 571 | __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 572 | break; \ |
| 573 | case 4: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 574 | __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 575 | break; \ |
| 576 | case 8: ON64( \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 577 | __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 578 | break; \ |
| 579 | } \ |
| 580 | } while (0) |
| 581 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 582 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 583 | enum x86_intercept intercept, |
| 584 | enum x86_intercept_stage stage) |
| 585 | { |
| 586 | struct x86_instruction_info info = { |
| 587 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 588 | .rep_prefix = ctxt->rep_prefix, |
| 589 | .modrm_mod = ctxt->modrm_mod, |
| 590 | .modrm_reg = ctxt->modrm_reg, |
| 591 | .modrm_rm = ctxt->modrm_rm, |
| 592 | .src_val = ctxt->src.val64, |
| 593 | .src_bytes = ctxt->src.bytes, |
| 594 | .dst_bytes = ctxt->dst.bytes, |
| 595 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 596 | .next_rip = ctxt->eip, |
| 597 | }; |
| 598 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 599 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 602 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 603 | { |
| 604 | *dest = (*dest & ~mask) | (src & mask); |
| 605 | } |
| 606 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 607 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 608 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 609 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 612 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 613 | { |
| 614 | u16 sel; |
| 615 | struct desc_struct ss; |
| 616 | |
| 617 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 618 | return ~0UL; |
| 619 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 620 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 621 | } |
| 622 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 623 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 624 | { |
| 625 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 626 | } |
| 627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 629 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 630 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 631 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 632 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 633 | return reg; |
| 634 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 635 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 639 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 640 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 641 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 644 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 645 | { |
| 646 | assign_masked(reg, *reg + inc, mask); |
| 647 | } |
| 648 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 649 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 650 | 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] | 651 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 652 | ulong mask; |
| 653 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 654 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 655 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 656 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 657 | mask = ad_mask(ctxt); |
| 658 | masked_increment(reg, mask, inc); |
| 659 | } |
| 660 | |
| 661 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 662 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 663 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 664 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 665 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 666 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 667 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 668 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 669 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 670 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 671 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 672 | { |
| 673 | u32 limit = get_desc_limit(desc); |
| 674 | |
| 675 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 676 | } |
| 677 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 678 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 679 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 680 | ctxt->has_seg_override = true; |
| 681 | ctxt->seg_override = seg; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 682 | } |
| 683 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 684 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 685 | { |
| 686 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 687 | return 0; |
| 688 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 689 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 690 | } |
| 691 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 692 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 693 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 694 | if (!ctxt->has_seg_override) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 695 | return 0; |
| 696 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 697 | return ctxt->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 698 | } |
| 699 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 700 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 701 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 702 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 703 | ctxt->exception.vector = vec; |
| 704 | ctxt->exception.error_code = error; |
| 705 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 706 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 707 | } |
| 708 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 709 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 710 | { |
| 711 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 712 | } |
| 713 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 714 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 715 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 716 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 717 | } |
| 718 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 719 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 720 | { |
| 721 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 722 | } |
| 723 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 724 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 725 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 726 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 727 | } |
| 728 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 729 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 730 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 731 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 732 | } |
| 733 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 734 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 735 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 736 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 737 | } |
| 738 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 739 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 740 | { |
| 741 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 742 | } |
| 743 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 744 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 745 | { |
| 746 | u16 selector; |
| 747 | struct desc_struct desc; |
| 748 | |
| 749 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 750 | return selector; |
| 751 | } |
| 752 | |
| 753 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 754 | unsigned seg) |
| 755 | { |
| 756 | u16 dummy; |
| 757 | u32 base3; |
| 758 | struct desc_struct desc; |
| 759 | |
| 760 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 761 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 762 | } |
| 763 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 764 | /* |
| 765 | * x86 defines three classes of vector instructions: explicitly |
| 766 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 767 | * depending on whether they're AVX encoded or not. |
| 768 | * |
| 769 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 770 | * subject to the same check. |
| 771 | */ |
| 772 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 773 | { |
| 774 | if (likely(size < 16)) |
| 775 | return false; |
| 776 | |
| 777 | if (ctxt->d & Aligned) |
| 778 | return true; |
| 779 | else if (ctxt->d & Unaligned) |
| 780 | return false; |
| 781 | else if (ctxt->d & Avx) |
| 782 | return false; |
| 783 | else |
| 784 | return true; |
| 785 | } |
| 786 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 787 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 788 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 789 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 790 | ulong *linear) |
| 791 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 792 | struct desc_struct desc; |
| 793 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 794 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 795 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 796 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 797 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 798 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 799 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 800 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 801 | case X86EMUL_MODE_PROT64: |
| 802 | if (((signed long)la << 16) >> 16 != la) |
| 803 | return emulate_gp(ctxt, 0); |
| 804 | break; |
| 805 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 806 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 807 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 808 | if (!usable) |
| 809 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 810 | /* code segment in protected mode or read-only data segment */ |
| 811 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 812 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 813 | goto bad; |
| 814 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 815 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 816 | goto bad; |
| 817 | lim = desc_limit_scaled(&desc); |
| 818 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 819 | /* expand-up segment */ |
| 820 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 821 | goto bad; |
| 822 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 823 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 824 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 825 | goto bad; |
| 826 | lim = desc.d ? 0xffffffff : 0xffff; |
| 827 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 828 | goto bad; |
| 829 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 830 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 831 | if (!(desc.type & 8)) { |
| 832 | /* data segment */ |
| 833 | if (cpl > desc.dpl) |
| 834 | goto bad; |
| 835 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 836 | /* nonconforming code segment */ |
| 837 | if (cpl != desc.dpl) |
| 838 | goto bad; |
| 839 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 840 | /* conforming code segment */ |
| 841 | if (cpl < desc.dpl) |
| 842 | goto bad; |
| 843 | } |
| 844 | break; |
| 845 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 846 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 847 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 848 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 849 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 850 | *linear = la; |
| 851 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 852 | bad: |
| 853 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 854 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 855 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 856 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 857 | } |
| 858 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 859 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 860 | struct segmented_address addr, |
| 861 | unsigned size, bool write, |
| 862 | ulong *linear) |
| 863 | { |
| 864 | return __linearize(ctxt, addr, size, write, false, linear); |
| 865 | } |
| 866 | |
| 867 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 868 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 869 | struct segmented_address addr, |
| 870 | void *data, |
| 871 | unsigned size) |
| 872 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 873 | int rc; |
| 874 | ulong linear; |
| 875 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 876 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 877 | if (rc != X86EMUL_CONTINUE) |
| 878 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 879 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 882 | /* |
| 883 | * Fetch the next byte of the instruction being emulated which is pointed to |
| 884 | * by ctxt->_eip, then increment ctxt->_eip. |
| 885 | * |
| 886 | * Also prefetch the remaining bytes of the instruction without crossing page |
| 887 | * boundary if they are not in fetch_cache yet. |
| 888 | */ |
| 889 | 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] | 890 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 891 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 892 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 893 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 894 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 895 | if (ctxt->_eip == fc->end) { |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 896 | unsigned long linear; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 897 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 898 | .ea = ctxt->_eip }; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 899 | cur_size = fc->end - fc->start; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 900 | size = min(15UL - cur_size, |
| 901 | PAGE_SIZE - offset_in_page(ctxt->_eip)); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 902 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 903 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 904 | return rc; |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 905 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 906 | size, &ctxt->exception); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 907 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 908 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 909 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 910 | } |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 911 | *dest = fc->data[ctxt->_eip - fc->start]; |
| 912 | ctxt->_eip++; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 913 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 917 | void *dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 918 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 919 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 920 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 921 | /* x86 instructions are limited to 15 bytes. */ |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 922 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 923 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 924 | while (size--) { |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 925 | rc = do_insn_fetch_byte(ctxt, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 926 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 927 | return rc; |
| 928 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 929 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 932 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 933 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 934 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 935 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 936 | if (rc != X86EMUL_CONTINUE) \ |
| 937 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 938 | (_type)_x; \ |
| 939 | }) |
| 940 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 941 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 942 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 943 | if (rc != X86EMUL_CONTINUE) \ |
| 944 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 945 | }) |
| 946 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 947 | /* |
| 948 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 949 | * pointer into the block that addresses the relevant register. |
| 950 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 951 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 952 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 953 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 954 | { |
| 955 | void *p; |
| 956 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 957 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 958 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 959 | else |
| 960 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 961 | return p; |
| 962 | } |
| 963 | |
| 964 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 965 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | u16 *size, unsigned long *address, int op_bytes) |
| 967 | { |
| 968 | int rc; |
| 969 | |
| 970 | if (op_bytes == 2) |
| 971 | op_bytes = 3; |
| 972 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 973 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 974 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 976 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 977 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 978 | return rc; |
| 979 | } |
| 980 | |
Avi Kivity | 34b7765 | 2013-01-19 19:51:56 +0200 | [diff] [blame] | 981 | FASTOP2(add); |
| 982 | FASTOP2(or); |
| 983 | FASTOP2(adc); |
| 984 | FASTOP2(sbb); |
| 985 | FASTOP2(and); |
| 986 | FASTOP2(sub); |
| 987 | FASTOP2(xor); |
| 988 | FASTOP2(cmp); |
| 989 | FASTOP2(test); |
| 990 | |
| 991 | FASTOP3WCL(shld); |
| 992 | FASTOP3WCL(shrd); |
| 993 | |
| 994 | FASTOP2W(imul); |
| 995 | |
| 996 | FASTOP1(not); |
| 997 | FASTOP1(neg); |
| 998 | FASTOP1(inc); |
| 999 | FASTOP1(dec); |
| 1000 | |
| 1001 | FASTOP2CL(rol); |
| 1002 | FASTOP2CL(ror); |
| 1003 | FASTOP2CL(rcl); |
| 1004 | FASTOP2CL(rcr); |
| 1005 | FASTOP2CL(shl); |
| 1006 | FASTOP2CL(shr); |
| 1007 | FASTOP2CL(sar); |
| 1008 | |
| 1009 | FASTOP2W(bsf); |
| 1010 | FASTOP2W(bsr); |
| 1011 | FASTOP2W(bt); |
| 1012 | FASTOP2W(bts); |
| 1013 | FASTOP2W(btr); |
| 1014 | FASTOP2W(btc); |
| 1015 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1016 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1017 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1018 | u8 rc; |
| 1019 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1020 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1021 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
Avi Kivity | 3f0c3d0 | 2013-01-26 23:56:04 +0200 | [diff] [blame] | 1022 | asm("push %[flags]; popf; call *%[fastop]" |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 1023 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 1024 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1025 | } |
| 1026 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1027 | static void fetch_register_operand(struct operand *op) |
| 1028 | { |
| 1029 | switch (op->bytes) { |
| 1030 | case 1: |
| 1031 | op->val = *(u8 *)op->addr.reg; |
| 1032 | break; |
| 1033 | case 2: |
| 1034 | op->val = *(u16 *)op->addr.reg; |
| 1035 | break; |
| 1036 | case 4: |
| 1037 | op->val = *(u32 *)op->addr.reg; |
| 1038 | break; |
| 1039 | case 8: |
| 1040 | op->val = *(u64 *)op->addr.reg; |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1045 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 1046 | { |
| 1047 | ctxt->ops->get_fpu(ctxt); |
| 1048 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1049 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 1050 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 1051 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 1052 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 1053 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 1054 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 1055 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 1056 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1057 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1058 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 1059 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 1060 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 1061 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 1062 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 1063 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 1064 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 1065 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1066 | #endif |
| 1067 | default: BUG(); |
| 1068 | } |
| 1069 | ctxt->ops->put_fpu(ctxt); |
| 1070 | } |
| 1071 | |
| 1072 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 1073 | int reg) |
| 1074 | { |
| 1075 | ctxt->ops->get_fpu(ctxt); |
| 1076 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1077 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 1078 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 1079 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 1080 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 1081 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 1082 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 1083 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 1084 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1085 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1086 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 1087 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1088 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1089 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1090 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1091 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1092 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1093 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1094 | #endif |
| 1095 | default: BUG(); |
| 1096 | } |
| 1097 | ctxt->ops->put_fpu(ctxt); |
| 1098 | } |
| 1099 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1100 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1101 | { |
| 1102 | ctxt->ops->get_fpu(ctxt); |
| 1103 | switch (reg) { |
| 1104 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1105 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1106 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1107 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1108 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1109 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1110 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1111 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1112 | default: BUG(); |
| 1113 | } |
| 1114 | ctxt->ops->put_fpu(ctxt); |
| 1115 | } |
| 1116 | |
| 1117 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1118 | { |
| 1119 | ctxt->ops->get_fpu(ctxt); |
| 1120 | switch (reg) { |
| 1121 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1122 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1123 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1124 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1125 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1126 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1127 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1128 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1129 | default: BUG(); |
| 1130 | } |
| 1131 | ctxt->ops->put_fpu(ctxt); |
| 1132 | } |
| 1133 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1134 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1135 | { |
| 1136 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1137 | return emulate_nm(ctxt); |
| 1138 | |
| 1139 | ctxt->ops->get_fpu(ctxt); |
| 1140 | asm volatile("fninit"); |
| 1141 | ctxt->ops->put_fpu(ctxt); |
| 1142 | return X86EMUL_CONTINUE; |
| 1143 | } |
| 1144 | |
| 1145 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1146 | { |
| 1147 | u16 fcw; |
| 1148 | |
| 1149 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1150 | return emulate_nm(ctxt); |
| 1151 | |
| 1152 | ctxt->ops->get_fpu(ctxt); |
| 1153 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1154 | ctxt->ops->put_fpu(ctxt); |
| 1155 | |
| 1156 | /* force 2 byte destination */ |
| 1157 | ctxt->dst.bytes = 2; |
| 1158 | ctxt->dst.val = fcw; |
| 1159 | |
| 1160 | return X86EMUL_CONTINUE; |
| 1161 | } |
| 1162 | |
| 1163 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1164 | { |
| 1165 | u16 fsw; |
| 1166 | |
| 1167 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1168 | return emulate_nm(ctxt); |
| 1169 | |
| 1170 | ctxt->ops->get_fpu(ctxt); |
| 1171 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1172 | ctxt->ops->put_fpu(ctxt); |
| 1173 | |
| 1174 | /* force 2 byte destination */ |
| 1175 | ctxt->dst.bytes = 2; |
| 1176 | ctxt->dst.val = fsw; |
| 1177 | |
| 1178 | return X86EMUL_CONTINUE; |
| 1179 | } |
| 1180 | |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1181 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1182 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1183 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1184 | unsigned reg = ctxt->modrm_reg; |
| 1185 | int highbyte_regs = ctxt->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1186 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1187 | if (!(ctxt->d & ModRM)) |
| 1188 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1189 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1190 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1191 | op->type = OP_XMM; |
| 1192 | op->bytes = 16; |
| 1193 | op->addr.xmm = reg; |
| 1194 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1195 | return; |
| 1196 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1197 | if (ctxt->d & Mmx) { |
| 1198 | reg &= 7; |
| 1199 | op->type = OP_MM; |
| 1200 | op->bytes = 8; |
| 1201 | op->addr.mm = reg; |
| 1202 | return; |
| 1203 | } |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1204 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1205 | op->type = OP_REG; |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1206 | if (ctxt->d & ByteOp) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1207 | op->addr.reg = decode_register(ctxt, reg, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1208 | op->bytes = 1; |
| 1209 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1210 | op->addr.reg = decode_register(ctxt, reg, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1211 | op->bytes = ctxt->op_bytes; |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1212 | } |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1213 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1214 | op->orig_val = op->val; |
| 1215 | } |
| 1216 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1217 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1218 | { |
| 1219 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1220 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1221 | } |
| 1222 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1223 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1224 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1225 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1226 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1227 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1228 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1229 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1230 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1231 | if (ctxt->rex_prefix) { |
| 1232 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ |
| 1233 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ |
| 1234 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1237 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
| 1238 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
| 1239 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); |
| 1240 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1241 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1242 | if (ctxt->modrm_mod == 3) { |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame^] | 1243 | int highbyte_regs = ctxt->rex_prefix == 0; |
| 1244 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1245 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1246 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Paolo Bonzini | 8acb4207 | 2013-05-30 16:35:55 +0200 | [diff] [blame^] | 1247 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, |
| 1248 | highbyte_regs && (ctxt->d & ByteOp)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1249 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1250 | op->type = OP_XMM; |
| 1251 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1252 | op->addr.xmm = ctxt->modrm_rm; |
| 1253 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1254 | return rc; |
| 1255 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1256 | if (ctxt->d & Mmx) { |
| 1257 | op->type = OP_MM; |
| 1258 | op->bytes = 8; |
| 1259 | op->addr.xmm = ctxt->modrm_rm & 7; |
| 1260 | return rc; |
| 1261 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1262 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1263 | return rc; |
| 1264 | } |
| 1265 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1266 | op->type = OP_MEM; |
| 1267 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1268 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1269 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1270 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1271 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1272 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1273 | |
| 1274 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1275 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1276 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1277 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1278 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1279 | break; |
| 1280 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1281 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1282 | break; |
| 1283 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1284 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1285 | break; |
| 1286 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1287 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1288 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1289 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1290 | break; |
| 1291 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1292 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1293 | break; |
| 1294 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1295 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1296 | break; |
| 1297 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1298 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1299 | break; |
| 1300 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1301 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1302 | break; |
| 1303 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1304 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1305 | break; |
| 1306 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1307 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1308 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1309 | break; |
| 1310 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1311 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1312 | break; |
| 1313 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1314 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1315 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1316 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1317 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1318 | } else { |
| 1319 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1320 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1321 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1322 | index_reg |= (sib >> 3) & 7; |
| 1323 | base_reg |= sib & 7; |
| 1324 | scale = sib >> 6; |
| 1325 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1326 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1327 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1328 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1329 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1330 | adjust_modrm_seg(ctxt, base_reg); |
| 1331 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1332 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1333 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1334 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1335 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1336 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1337 | } else { |
| 1338 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1339 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1340 | adjust_modrm_seg(ctxt, base_reg); |
| 1341 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1342 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1343 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1344 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1345 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1346 | break; |
| 1347 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1348 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1349 | break; |
| 1350 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1351 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1352 | break; |
| 1353 | } |
| 1354 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1355 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1356 | done: |
| 1357 | return rc; |
| 1358 | } |
| 1359 | |
| 1360 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1361 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1362 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1363 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1364 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1365 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1366 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1367 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1368 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1369 | break; |
| 1370 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1371 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1372 | break; |
| 1373 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1374 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1375 | break; |
| 1376 | } |
| 1377 | done: |
| 1378 | return rc; |
| 1379 | } |
| 1380 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1381 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1382 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1383 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1384 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1385 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
| 1386 | mask = ~(ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1387 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1388 | if (ctxt->src.bytes == 2) |
| 1389 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1390 | else if (ctxt->src.bytes == 4) |
| 1391 | sv = (s32)ctxt->src.val & (s32)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1392 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1393 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1394 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1395 | |
| 1396 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1397 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1400 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1401 | unsigned long addr, void *dest, unsigned size) |
| 1402 | { |
| 1403 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1404 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1405 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1406 | if (mc->pos < mc->end) |
| 1407 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1408 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1409 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1410 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1411 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1412 | &ctxt->exception); |
| 1413 | if (rc != X86EMUL_CONTINUE) |
| 1414 | return rc; |
| 1415 | |
| 1416 | mc->end += size; |
| 1417 | |
| 1418 | read_cached: |
| 1419 | memcpy(dest, mc->data + mc->pos, size); |
| 1420 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1421 | return X86EMUL_CONTINUE; |
| 1422 | } |
| 1423 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1424 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1425 | struct segmented_address addr, |
| 1426 | void *data, |
| 1427 | unsigned size) |
| 1428 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1429 | int rc; |
| 1430 | ulong linear; |
| 1431 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1432 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1433 | if (rc != X86EMUL_CONTINUE) |
| 1434 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1435 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1439 | struct segmented_address addr, |
| 1440 | const void *data, |
| 1441 | unsigned size) |
| 1442 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1443 | int rc; |
| 1444 | ulong linear; |
| 1445 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1446 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1447 | if (rc != X86EMUL_CONTINUE) |
| 1448 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1449 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1450 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1454 | struct segmented_address addr, |
| 1455 | const void *orig_data, const void *data, |
| 1456 | unsigned size) |
| 1457 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1458 | int rc; |
| 1459 | ulong linear; |
| 1460 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1461 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1462 | if (rc != X86EMUL_CONTINUE) |
| 1463 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1464 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1465 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1468 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1469 | unsigned int size, unsigned short port, |
| 1470 | void *dest) |
| 1471 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1472 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1473 | |
| 1474 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1475 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1476 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1477 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1478 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1479 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1480 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1481 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1482 | count); |
| 1483 | if (n == 0) |
| 1484 | n = 1; |
| 1485 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1486 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1487 | return 0; |
| 1488 | rc->end = n * size; |
| 1489 | } |
| 1490 | |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1491 | if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) { |
| 1492 | ctxt->dst.data = rc->data + rc->pos; |
| 1493 | ctxt->dst.type = OP_MEM_STR; |
| 1494 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1495 | rc->pos = rc->end; |
| 1496 | } else { |
| 1497 | memcpy(dest, rc->data + rc->pos, size); |
| 1498 | rc->pos += size; |
| 1499 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1500 | return 1; |
| 1501 | } |
| 1502 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1503 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1504 | u16 index, struct desc_struct *desc) |
| 1505 | { |
| 1506 | struct desc_ptr dt; |
| 1507 | ulong addr; |
| 1508 | |
| 1509 | ctxt->ops->get_idt(ctxt, &dt); |
| 1510 | |
| 1511 | if (dt.size < index * 8 + 7) |
| 1512 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1513 | |
| 1514 | addr = dt.address + index * 8; |
| 1515 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1516 | &ctxt->exception); |
| 1517 | } |
| 1518 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1519 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1520 | u16 selector, struct desc_ptr *dt) |
| 1521 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1522 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1523 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1524 | if (selector & 1 << 2) { |
| 1525 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1526 | u16 sel; |
| 1527 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1528 | memset (dt, 0, sizeof *dt); |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1529 | if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1530 | return; |
| 1531 | |
| 1532 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1533 | dt->address = get_desc_base(&desc); |
| 1534 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1535 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /* allowed just for 8 bytes segments */ |
| 1539 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1540 | u16 selector, struct desc_struct *desc, |
| 1541 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1542 | { |
| 1543 | struct desc_ptr dt; |
| 1544 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1545 | ulong addr; |
| 1546 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1547 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1548 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1549 | if (dt.size < index * 8 + 7) |
| 1550 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1551 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1552 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1553 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1554 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | /* allowed just for 8 bytes segments */ |
| 1558 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1559 | u16 selector, struct desc_struct *desc) |
| 1560 | { |
| 1561 | struct desc_ptr dt; |
| 1562 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1563 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1564 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1565 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1566 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1567 | if (dt.size < index * 8 + 7) |
| 1568 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1569 | |
| 1570 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1571 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1572 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1573 | } |
| 1574 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1575 | /* Does not support long mode */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1576 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1577 | u16 selector, int seg) |
| 1578 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1579 | struct desc_struct seg_desc, old_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1580 | u8 dpl, rpl, cpl; |
| 1581 | unsigned err_vec = GP_VECTOR; |
| 1582 | u32 err_code = 0; |
| 1583 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1584 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1585 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1586 | u16 dummy; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1587 | |
| 1588 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1589 | |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1590 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
| 1591 | /* set real mode segment descriptor (keep limit etc. for |
| 1592 | * unreal mode) */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1593 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1594 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1595 | goto load; |
Kevin Wolf | f8da94e | 2013-04-11 14:06:03 +0200 | [diff] [blame] | 1596 | } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) { |
| 1597 | /* VM86 needs a clean new segment descriptor */ |
| 1598 | set_desc_base(&seg_desc, selector << 4); |
| 1599 | set_desc_limit(&seg_desc, 0xffff); |
| 1600 | seg_desc.type = 3; |
| 1601 | seg_desc.p = 1; |
| 1602 | seg_desc.s = 1; |
| 1603 | seg_desc.dpl = 3; |
| 1604 | goto load; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1607 | rpl = selector & 3; |
| 1608 | cpl = ctxt->ops->cpl(ctxt); |
| 1609 | |
| 1610 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1611 | if ((seg == VCPU_SREG_CS |
| 1612 | || (seg == VCPU_SREG_SS |
| 1613 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1614 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1615 | && null_selector) |
| 1616 | goto exception; |
| 1617 | |
| 1618 | /* TR should be in GDT only */ |
| 1619 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1620 | goto exception; |
| 1621 | |
| 1622 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1623 | goto load; |
| 1624 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1625 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1626 | if (ret != X86EMUL_CONTINUE) |
| 1627 | return ret; |
| 1628 | |
| 1629 | err_code = selector & 0xfffc; |
| 1630 | err_vec = GP_VECTOR; |
| 1631 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1632 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1633 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1634 | goto exception; |
| 1635 | |
| 1636 | if (!seg_desc.p) { |
| 1637 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1638 | goto exception; |
| 1639 | } |
| 1640 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1641 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1642 | |
| 1643 | switch (seg) { |
| 1644 | case VCPU_SREG_SS: |
| 1645 | /* |
| 1646 | * segment is not a writable data segment or segment |
| 1647 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1648 | */ |
| 1649 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1650 | goto exception; |
| 1651 | break; |
| 1652 | case VCPU_SREG_CS: |
| 1653 | if (!(seg_desc.type & 8)) |
| 1654 | goto exception; |
| 1655 | |
| 1656 | if (seg_desc.type & 4) { |
| 1657 | /* conforming */ |
| 1658 | if (dpl > cpl) |
| 1659 | goto exception; |
| 1660 | } else { |
| 1661 | /* nonconforming */ |
| 1662 | if (rpl > cpl || dpl != cpl) |
| 1663 | goto exception; |
| 1664 | } |
| 1665 | /* CS(RPL) <- CPL */ |
| 1666 | selector = (selector & 0xfffc) | cpl; |
| 1667 | break; |
| 1668 | case VCPU_SREG_TR: |
| 1669 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1670 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1671 | old_desc = seg_desc; |
| 1672 | seg_desc.type |= 2; /* busy */ |
| 1673 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1674 | sizeof(seg_desc), &ctxt->exception); |
| 1675 | if (ret != X86EMUL_CONTINUE) |
| 1676 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1677 | break; |
| 1678 | case VCPU_SREG_LDTR: |
| 1679 | if (seg_desc.s || seg_desc.type != 2) |
| 1680 | goto exception; |
| 1681 | break; |
| 1682 | default: /* DS, ES, FS, or GS */ |
| 1683 | /* |
| 1684 | * segment is not a data or readable code segment or |
| 1685 | * ((segment is a data or nonconforming code segment) |
| 1686 | * and (both RPL and CPL > DPL)) |
| 1687 | */ |
| 1688 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1689 | (((seg_desc.type & 0xc) != 0xc) && |
| 1690 | (rpl > dpl && cpl > dpl))) |
| 1691 | goto exception; |
| 1692 | break; |
| 1693 | } |
| 1694 | |
| 1695 | if (seg_desc.s) { |
| 1696 | /* mark segment as accessed */ |
| 1697 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1698 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1699 | if (ret != X86EMUL_CONTINUE) |
| 1700 | return ret; |
| 1701 | } |
| 1702 | load: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1703 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1704 | return X86EMUL_CONTINUE; |
| 1705 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1706 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1707 | return X86EMUL_PROPAGATE_FAULT; |
| 1708 | } |
| 1709 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1710 | static void write_register_operand(struct operand *op) |
| 1711 | { |
| 1712 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1713 | switch (op->bytes) { |
| 1714 | case 1: |
| 1715 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1716 | break; |
| 1717 | case 2: |
| 1718 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1719 | break; |
| 1720 | case 4: |
| 1721 | *op->addr.reg = (u32)op->val; |
| 1722 | break; /* 64b: zero-extend */ |
| 1723 | case 8: |
| 1724 | *op->addr.reg = op->val; |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
| 1728 | |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 1729 | static int writeback(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1730 | { |
| 1731 | int rc; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1732 | |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 1733 | if (ctxt->d & NoWrite) |
| 1734 | return X86EMUL_CONTINUE; |
| 1735 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1736 | switch (ctxt->dst.type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1737 | case OP_REG: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1738 | write_register_operand(&ctxt->dst); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1739 | break; |
| 1740 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1741 | if (ctxt->lock_prefix) |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1742 | rc = segmented_cmpxchg(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1743 | ctxt->dst.addr.mem, |
| 1744 | &ctxt->dst.orig_val, |
| 1745 | &ctxt->dst.val, |
| 1746 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1747 | else |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1748 | rc = segmented_write(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1749 | ctxt->dst.addr.mem, |
| 1750 | &ctxt->dst.val, |
| 1751 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1752 | if (rc != X86EMUL_CONTINUE) |
| 1753 | return rc; |
| 1754 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1755 | case OP_MEM_STR: |
| 1756 | rc = segmented_write(ctxt, |
| 1757 | ctxt->dst.addr.mem, |
| 1758 | ctxt->dst.data, |
| 1759 | ctxt->dst.bytes * ctxt->dst.count); |
| 1760 | if (rc != X86EMUL_CONTINUE) |
| 1761 | return rc; |
| 1762 | break; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1763 | case OP_XMM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1764 | write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm); |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1765 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1766 | case OP_MM: |
| 1767 | write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm); |
| 1768 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1769 | case OP_NONE: |
| 1770 | /* no writeback */ |
| 1771 | break; |
| 1772 | default: |
| 1773 | break; |
| 1774 | } |
| 1775 | return X86EMUL_CONTINUE; |
| 1776 | } |
| 1777 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1778 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1779 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1780 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1781 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1782 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1783 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1784 | addr.seg = VCPU_SREG_SS; |
| 1785 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1786 | return segmented_write(ctxt, addr, data, bytes); |
| 1787 | } |
| 1788 | |
| 1789 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1790 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1791 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1792 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1793 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1796 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1797 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1798 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1799 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1800 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1801 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1802 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1803 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1804 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1805 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1806 | return rc; |
| 1807 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1808 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1809 | return rc; |
| 1810 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1811 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1812 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1813 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1814 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1815 | } |
| 1816 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1817 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1818 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1819 | { |
| 1820 | int rc; |
| 1821 | unsigned long val, change_mask; |
| 1822 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1823 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1824 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1825 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1826 | if (rc != X86EMUL_CONTINUE) |
| 1827 | return rc; |
| 1828 | |
| 1829 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1830 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1831 | |
| 1832 | switch(ctxt->mode) { |
| 1833 | case X86EMUL_MODE_PROT64: |
| 1834 | case X86EMUL_MODE_PROT32: |
| 1835 | case X86EMUL_MODE_PROT16: |
| 1836 | if (cpl == 0) |
| 1837 | change_mask |= EFLG_IOPL; |
| 1838 | if (cpl <= iopl) |
| 1839 | change_mask |= EFLG_IF; |
| 1840 | break; |
| 1841 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1842 | if (iopl < 3) |
| 1843 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1844 | change_mask |= EFLG_IF; |
| 1845 | break; |
| 1846 | default: /* real mode */ |
| 1847 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1848 | break; |
| 1849 | } |
| 1850 | |
| 1851 | *(unsigned long *)dest = |
| 1852 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1853 | |
| 1854 | return rc; |
| 1855 | } |
| 1856 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1857 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1858 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1859 | ctxt->dst.type = OP_REG; |
| 1860 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1861 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1862 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1863 | } |
| 1864 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1865 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1866 | { |
| 1867 | int rc; |
| 1868 | unsigned frame_size = ctxt->src.val; |
| 1869 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1870 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1871 | |
| 1872 | if (nesting_level) |
| 1873 | return X86EMUL_UNHANDLEABLE; |
| 1874 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1875 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1876 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1877 | if (rc != X86EMUL_CONTINUE) |
| 1878 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1879 | 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] | 1880 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1881 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1882 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1883 | stack_mask(ctxt)); |
| 1884 | return X86EMUL_CONTINUE; |
| 1885 | } |
| 1886 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1887 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1888 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1889 | 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] | 1890 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1891 | 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] | 1892 | } |
| 1893 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1894 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1895 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1896 | int seg = ctxt->src2.val; |
| 1897 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1898 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1899 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1900 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1901 | } |
| 1902 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1903 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1904 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1905 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1906 | unsigned long selector; |
| 1907 | int rc; |
| 1908 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1909 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1910 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1911 | return rc; |
| 1912 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1913 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1914 | return rc; |
| 1915 | } |
| 1916 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1917 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1918 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1919 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1920 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1921 | int reg = VCPU_REGS_RAX; |
| 1922 | |
| 1923 | while (reg <= VCPU_REGS_RDI) { |
| 1924 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1925 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1926 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1927 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1928 | if (rc != X86EMUL_CONTINUE) |
| 1929 | return rc; |
| 1930 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1931 | ++reg; |
| 1932 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1933 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1934 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1937 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1938 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1939 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1940 | return em_push(ctxt); |
| 1941 | } |
| 1942 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1943 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1944 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1945 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1946 | int reg = VCPU_REGS_RDI; |
| 1947 | |
| 1948 | while (reg >= VCPU_REGS_RAX) { |
| 1949 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1950 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1951 | --reg; |
| 1952 | } |
| 1953 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1954 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1955 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1956 | break; |
| 1957 | --reg; |
| 1958 | } |
| 1959 | return rc; |
| 1960 | } |
| 1961 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1962 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1963 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1964 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1965 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1966 | struct desc_ptr dt; |
| 1967 | gva_t cs_addr; |
| 1968 | gva_t eip_addr; |
| 1969 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1970 | |
| 1971 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1972 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1973 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1974 | if (rc != X86EMUL_CONTINUE) |
| 1975 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1976 | |
| 1977 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1978 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1979 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1980 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1981 | if (rc != X86EMUL_CONTINUE) |
| 1982 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1983 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1984 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1985 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1986 | if (rc != X86EMUL_CONTINUE) |
| 1987 | return rc; |
| 1988 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1989 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1990 | |
| 1991 | eip_addr = dt.address + (irq << 2); |
| 1992 | cs_addr = dt.address + (irq << 2) + 2; |
| 1993 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1994 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1995 | if (rc != X86EMUL_CONTINUE) |
| 1996 | return rc; |
| 1997 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1998 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1999 | if (rc != X86EMUL_CONTINUE) |
| 2000 | return rc; |
| 2001 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2002 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2003 | if (rc != X86EMUL_CONTINUE) |
| 2004 | return rc; |
| 2005 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2006 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2007 | |
| 2008 | return rc; |
| 2009 | } |
| 2010 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2011 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 2012 | { |
| 2013 | int rc; |
| 2014 | |
| 2015 | invalidate_registers(ctxt); |
| 2016 | rc = __emulate_int_real(ctxt, irq); |
| 2017 | if (rc == X86EMUL_CONTINUE) |
| 2018 | writeback_registers(ctxt); |
| 2019 | return rc; |
| 2020 | } |
| 2021 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2022 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2023 | { |
| 2024 | switch(ctxt->mode) { |
| 2025 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2026 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 2027 | case X86EMUL_MODE_VM86: |
| 2028 | case X86EMUL_MODE_PROT16: |
| 2029 | case X86EMUL_MODE_PROT32: |
| 2030 | case X86EMUL_MODE_PROT64: |
| 2031 | default: |
| 2032 | /* Protected mode interrupts unimplemented yet */ |
| 2033 | return X86EMUL_UNHANDLEABLE; |
| 2034 | } |
| 2035 | } |
| 2036 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2037 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2038 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2039 | int rc = X86EMUL_CONTINUE; |
| 2040 | unsigned long temp_eip = 0; |
| 2041 | unsigned long temp_eflags = 0; |
| 2042 | unsigned long cs = 0; |
| 2043 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 2044 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 2045 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 2046 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 2047 | |
| 2048 | /* TODO: Add stack limit check */ |
| 2049 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2050 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2051 | |
| 2052 | if (rc != X86EMUL_CONTINUE) |
| 2053 | return rc; |
| 2054 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2055 | if (temp_eip & ~0xffff) |
| 2056 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2057 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2058 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2059 | |
| 2060 | if (rc != X86EMUL_CONTINUE) |
| 2061 | return rc; |
| 2062 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2063 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2064 | |
| 2065 | if (rc != X86EMUL_CONTINUE) |
| 2066 | return rc; |
| 2067 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2068 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2069 | |
| 2070 | if (rc != X86EMUL_CONTINUE) |
| 2071 | return rc; |
| 2072 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2073 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2074 | |
| 2075 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2076 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2077 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2078 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2079 | ctxt->eflags &= ~0xffff; |
| 2080 | ctxt->eflags |= temp_eflags; |
| 2081 | } |
| 2082 | |
| 2083 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 2084 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 2085 | |
| 2086 | return rc; |
| 2087 | } |
| 2088 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2089 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2090 | { |
| 2091 | switch(ctxt->mode) { |
| 2092 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2093 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2094 | case X86EMUL_MODE_VM86: |
| 2095 | case X86EMUL_MODE_PROT16: |
| 2096 | case X86EMUL_MODE_PROT32: |
| 2097 | case X86EMUL_MODE_PROT64: |
| 2098 | default: |
| 2099 | /* iret from protected mode unimplemented yet */ |
| 2100 | return X86EMUL_UNHANDLEABLE; |
| 2101 | } |
| 2102 | } |
| 2103 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2104 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2105 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2106 | int rc; |
| 2107 | unsigned short sel; |
| 2108 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2109 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2110 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2111 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2112 | if (rc != X86EMUL_CONTINUE) |
| 2113 | return rc; |
| 2114 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2115 | ctxt->_eip = 0; |
| 2116 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2117 | return X86EMUL_CONTINUE; |
| 2118 | } |
| 2119 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 2120 | static int em_mul_ex(struct x86_emulate_ctxt *ctxt) |
| 2121 | { |
| 2122 | u8 ex = 0; |
| 2123 | |
| 2124 | emulate_1op_rax_rdx(ctxt, "mul", ex); |
| 2125 | return X86EMUL_CONTINUE; |
| 2126 | } |
| 2127 | |
| 2128 | static int em_imul_ex(struct x86_emulate_ctxt *ctxt) |
| 2129 | { |
| 2130 | u8 ex = 0; |
| 2131 | |
| 2132 | emulate_1op_rax_rdx(ctxt, "imul", ex); |
| 2133 | return X86EMUL_CONTINUE; |
| 2134 | } |
| 2135 | |
| 2136 | static int em_div_ex(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2137 | { |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 2138 | u8 de = 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2139 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 2140 | emulate_1op_rax_rdx(ctxt, "div", de); |
| 2141 | if (de) |
| 2142 | return emulate_de(ctxt); |
| 2143 | return X86EMUL_CONTINUE; |
| 2144 | } |
| 2145 | |
| 2146 | static int em_idiv_ex(struct x86_emulate_ctxt *ctxt) |
| 2147 | { |
| 2148 | u8 de = 0; |
| 2149 | |
| 2150 | emulate_1op_rax_rdx(ctxt, "idiv", de); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 2151 | if (de) |
| 2152 | return emulate_de(ctxt); |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 2153 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 2156 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2157 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2158 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2159 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2160 | switch (ctxt->modrm_reg) { |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2161 | case 2: /* call near abs */ { |
| 2162 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2163 | old_eip = ctxt->_eip; |
| 2164 | ctxt->_eip = ctxt->src.val; |
| 2165 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2166 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2167 | break; |
| 2168 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2169 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2170 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2171 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2172 | case 5: /* jmp far */ |
| 2173 | rc = em_jmp_far(ctxt); |
| 2174 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2175 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2176 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2177 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2178 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2179 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2182 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2183 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2184 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2185 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2186 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2187 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2188 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2189 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2190 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2191 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2192 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2193 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2194 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2195 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2196 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2197 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2200 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2201 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2202 | ctxt->dst.type = OP_REG; |
| 2203 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2204 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2205 | return em_pop(ctxt); |
| 2206 | } |
| 2207 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2208 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2209 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2210 | int rc; |
| 2211 | unsigned long cs; |
| 2212 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2213 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2214 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2215 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2216 | if (ctxt->op_bytes == 4) |
| 2217 | ctxt->_eip = (u32)ctxt->_eip; |
| 2218 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2219 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2220 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2221 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2222 | return rc; |
| 2223 | } |
| 2224 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2225 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2226 | { |
| 2227 | /* Save real source value, then compare EAX against destination. */ |
| 2228 | ctxt->src.orig_val = ctxt->src.val; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2229 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX); |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2230 | fastop(ctxt, em_cmp); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2231 | |
| 2232 | if (ctxt->eflags & EFLG_ZF) { |
| 2233 | /* Success: write back to memory. */ |
| 2234 | ctxt->dst.val = ctxt->src.orig_val; |
| 2235 | } else { |
| 2236 | /* Failure: write the value we saw to EAX. */ |
| 2237 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2238 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2239 | } |
| 2240 | return X86EMUL_CONTINUE; |
| 2241 | } |
| 2242 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2243 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2244 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2245 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2246 | unsigned short sel; |
| 2247 | int rc; |
| 2248 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2249 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2250 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2251 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2252 | if (rc != X86EMUL_CONTINUE) |
| 2253 | return rc; |
| 2254 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2255 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2256 | return rc; |
| 2257 | } |
| 2258 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2259 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2260 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2261 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2262 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2263 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2264 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2265 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2266 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2267 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2268 | cs->s = 1; |
| 2269 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2270 | cs->p = 1; |
| 2271 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2272 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2273 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2274 | set_desc_base(ss, 0); /* flat segment */ |
| 2275 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2276 | ss->g = 1; /* 4kb granularity */ |
| 2277 | ss->s = 1; |
| 2278 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2279 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2280 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2281 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2282 | ss->l = 0; |
| 2283 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2286 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2287 | { |
| 2288 | u32 eax, ebx, ecx, edx; |
| 2289 | |
| 2290 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2291 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2292 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2293 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2294 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2295 | } |
| 2296 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2297 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2298 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2299 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2300 | u32 eax, ebx, ecx, edx; |
| 2301 | |
| 2302 | /* |
| 2303 | * syscall should always be enabled in longmode - so only become |
| 2304 | * vendor specific (cpuid) if other modes are active... |
| 2305 | */ |
| 2306 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2307 | return true; |
| 2308 | |
| 2309 | eax = 0x00000000; |
| 2310 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2311 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2312 | /* |
| 2313 | * Intel ("GenuineIntel") |
| 2314 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2315 | * longmode. Also an 64bit guest with a |
| 2316 | * 32bit compat-app running will #UD !! While this |
| 2317 | * behaviour can be fixed (by emulating) into AMD |
| 2318 | * response - CPUs of AMD can't behave like Intel. |
| 2319 | */ |
| 2320 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2321 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2322 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2323 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2324 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2325 | /* AMD ("AuthenticAMD") */ |
| 2326 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2327 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2328 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2329 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2330 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2331 | /* AMD ("AMDisbetter!") */ |
| 2332 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2333 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2334 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2335 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2336 | |
| 2337 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2341 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2342 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2343 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2344 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2345 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2346 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2347 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2348 | |
| 2349 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2350 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2351 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2352 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2353 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2354 | if (!(em_syscall_is_enabled(ctxt))) |
| 2355 | return emulate_ud(ctxt); |
| 2356 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2357 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2358 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2359 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2360 | if (!(efer & EFER_SCE)) |
| 2361 | return emulate_ud(ctxt); |
| 2362 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2363 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2364 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2365 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2366 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2367 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2368 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2369 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2370 | cs.l = 1; |
| 2371 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2372 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2373 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2374 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2375 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2376 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2377 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2378 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2379 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2380 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2381 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2382 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2383 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2384 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2385 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2386 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2387 | #endif |
| 2388 | } else { |
| 2389 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2390 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2391 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2392 | |
| 2393 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2394 | } |
| 2395 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2396 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2399 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2400 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2401 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2402 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2403 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2404 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2405 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2406 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2407 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2408 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2409 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2410 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2411 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2412 | /* |
| 2413 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2414 | * mode). |
| 2415 | */ |
| 2416 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2417 | && !vendor_intel(ctxt)) |
| 2418 | return emulate_ud(ctxt); |
| 2419 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2420 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2421 | * Therefore, we inject an #UD. |
| 2422 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2423 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2424 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2425 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2426 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2427 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2428 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2429 | switch (ctxt->mode) { |
| 2430 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2431 | if ((msr_data & 0xfffc) == 0x0) |
| 2432 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2433 | break; |
| 2434 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2435 | if (msr_data == 0x0) |
| 2436 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2437 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2438 | default: |
| 2439 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2443 | cs_sel = (u16)msr_data; |
| 2444 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2445 | ss_sel = cs_sel + 8; |
| 2446 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2447 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2448 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2449 | cs.l = 1; |
| 2450 | } |
| 2451 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2452 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2453 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2454 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2455 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2456 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2457 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2458 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2459 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2460 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2461 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2462 | } |
| 2463 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2464 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2465 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2466 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2467 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2468 | u64 msr_data; |
| 2469 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2470 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2471 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2472 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2473 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2474 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2475 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2476 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2477 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2478 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2479 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2480 | usermode = X86EMUL_MODE_PROT64; |
| 2481 | else |
| 2482 | usermode = X86EMUL_MODE_PROT32; |
| 2483 | |
| 2484 | cs.dpl = 3; |
| 2485 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2486 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2487 | switch (usermode) { |
| 2488 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2489 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2490 | if ((msr_data & 0xfffc) == 0x0) |
| 2491 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2492 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2493 | break; |
| 2494 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2495 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2496 | if (msr_data == 0x0) |
| 2497 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2498 | ss_sel = cs_sel + 8; |
| 2499 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2500 | cs.l = 1; |
| 2501 | break; |
| 2502 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2503 | cs_sel |= SELECTOR_RPL_MASK; |
| 2504 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2505 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2506 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2507 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2508 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2509 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2510 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2511 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2512 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2515 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2516 | { |
| 2517 | int iopl; |
| 2518 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2519 | return false; |
| 2520 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2521 | return true; |
| 2522 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2523 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2527 | u16 port, u16 len) |
| 2528 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2529 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2530 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2531 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2532 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2533 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2534 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2535 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2536 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2537 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2538 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2539 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2540 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2541 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2542 | base = get_desc_base(&tr_seg); |
| 2543 | #ifdef CONFIG_X86_64 |
| 2544 | base |= ((u64)base3) << 32; |
| 2545 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2546 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2547 | if (r != X86EMUL_CONTINUE) |
| 2548 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2549 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2550 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2551 | 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] | 2552 | if (r != X86EMUL_CONTINUE) |
| 2553 | return false; |
| 2554 | if ((perm >> bit_idx) & mask) |
| 2555 | return false; |
| 2556 | return true; |
| 2557 | } |
| 2558 | |
| 2559 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2560 | u16 port, u16 len) |
| 2561 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2562 | if (ctxt->perm_ok) |
| 2563 | return true; |
| 2564 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2565 | if (emulator_bad_iopl(ctxt)) |
| 2566 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2567 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2568 | |
| 2569 | ctxt->perm_ok = true; |
| 2570 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2571 | return true; |
| 2572 | } |
| 2573 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2574 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2575 | struct tss_segment_16 *tss) |
| 2576 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2577 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2578 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2579 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2580 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2581 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2582 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2583 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2584 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2585 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2586 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2587 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2588 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2589 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2590 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2591 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2592 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2596 | struct tss_segment_16 *tss) |
| 2597 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | int ret; |
| 2599 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2600 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2601 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2602 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2603 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2604 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2605 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2606 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2607 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2608 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2609 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2610 | |
| 2611 | /* |
| 2612 | * SDM says that segment selectors are loaded before segment |
| 2613 | * descriptors |
| 2614 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2615 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2616 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2617 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2618 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2619 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2620 | |
| 2621 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2622 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2623 | * it is handled in a context of new task |
| 2624 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2625 | ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2626 | if (ret != X86EMUL_CONTINUE) |
| 2627 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2628 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2629 | if (ret != X86EMUL_CONTINUE) |
| 2630 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2631 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2632 | if (ret != X86EMUL_CONTINUE) |
| 2633 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2634 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2635 | if (ret != X86EMUL_CONTINUE) |
| 2636 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2637 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2638 | if (ret != X86EMUL_CONTINUE) |
| 2639 | return ret; |
| 2640 | |
| 2641 | return X86EMUL_CONTINUE; |
| 2642 | } |
| 2643 | |
| 2644 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2645 | u16 tss_selector, u16 old_tss_sel, |
| 2646 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2647 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2648 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2649 | struct tss_segment_16 tss_seg; |
| 2650 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2651 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2652 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2653 | 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] | 2654 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2655 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2656 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2657 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2658 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2659 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2660 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2661 | 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] | 2662 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2663 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2664 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2665 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2666 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2667 | 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] | 2668 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2669 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2670 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2671 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2672 | |
| 2673 | if (old_tss_sel != 0xffff) { |
| 2674 | tss_seg.prev_task_link = old_tss_sel; |
| 2675 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2676 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2677 | &tss_seg.prev_task_link, |
| 2678 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2679 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2680 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2681 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2682 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2685 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2689 | struct tss_segment_32 *tss) |
| 2690 | { |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2691 | tss->cr3 = ctxt->ops->get_cr(ctxt, 3); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2692 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2693 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2694 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2695 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2696 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2697 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2698 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2699 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2700 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2701 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2702 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2703 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2704 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2705 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2706 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2707 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2708 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
| 2709 | tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2713 | struct tss_segment_32 *tss) |
| 2714 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2715 | int ret; |
| 2716 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2717 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2718 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2719 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2720 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2721 | |
| 2722 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2723 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2724 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2725 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2726 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2727 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2728 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2729 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2730 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2731 | |
| 2732 | /* |
| 2733 | * SDM says that segment selectors are loaded before segment |
| 2734 | * descriptors |
| 2735 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2736 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2737 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2738 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2739 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2740 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2741 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2742 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2743 | |
| 2744 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2745 | * If we're switching between Protected Mode and VM86, we need to make |
| 2746 | * sure to update the mode before loading the segment descriptors so |
| 2747 | * that the selectors are interpreted correctly. |
| 2748 | * |
| 2749 | * Need to get rflags to the vcpu struct immediately because it |
| 2750 | * influences the CPL which is checked at least when loading the segment |
| 2751 | * descriptors and when pushing an error code to the new kernel stack. |
| 2752 | * |
| 2753 | * TODO Introduce a separate ctxt->ops->set_cpl callback |
| 2754 | */ |
| 2755 | if (ctxt->eflags & X86_EFLAGS_VM) |
| 2756 | ctxt->mode = X86EMUL_MODE_VM86; |
| 2757 | else |
| 2758 | ctxt->mode = X86EMUL_MODE_PROT32; |
| 2759 | |
| 2760 | ctxt->ops->set_rflags(ctxt, ctxt->eflags); |
| 2761 | |
| 2762 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2763 | * Now load segment descriptors. If fault happenes at this stage |
| 2764 | * it is handled in a context of new task |
| 2765 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2766 | ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2767 | if (ret != X86EMUL_CONTINUE) |
| 2768 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2769 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2770 | if (ret != X86EMUL_CONTINUE) |
| 2771 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2772 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2773 | if (ret != X86EMUL_CONTINUE) |
| 2774 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2775 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2776 | if (ret != X86EMUL_CONTINUE) |
| 2777 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2778 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2779 | if (ret != X86EMUL_CONTINUE) |
| 2780 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2781 | ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2782 | if (ret != X86EMUL_CONTINUE) |
| 2783 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2784 | ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2785 | if (ret != X86EMUL_CONTINUE) |
| 2786 | return ret; |
| 2787 | |
| 2788 | return X86EMUL_CONTINUE; |
| 2789 | } |
| 2790 | |
| 2791 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2792 | u16 tss_selector, u16 old_tss_sel, |
| 2793 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2794 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2795 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2796 | struct tss_segment_32 tss_seg; |
| 2797 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2798 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2799 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2800 | 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] | 2801 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2802 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2803 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2804 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2805 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2806 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2807 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2808 | 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] | 2809 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2810 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2811 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2812 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2813 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2814 | 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] | 2815 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2816 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2817 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2818 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2819 | |
| 2820 | if (old_tss_sel != 0xffff) { |
| 2821 | tss_seg.prev_task_link = old_tss_sel; |
| 2822 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2823 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2824 | &tss_seg.prev_task_link, |
| 2825 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2826 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2827 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2828 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2829 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2830 | } |
| 2831 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2832 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2836 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2837 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2838 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2839 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2840 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2841 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2842 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2843 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2844 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2845 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2846 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2847 | |
| 2848 | /* FIXME: old_tss_base == ~0 ? */ |
| 2849 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2850 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2851 | if (ret != X86EMUL_CONTINUE) |
| 2852 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2853 | 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] | 2854 | if (ret != X86EMUL_CONTINUE) |
| 2855 | return ret; |
| 2856 | |
| 2857 | /* FIXME: check that next_tss_desc is tss */ |
| 2858 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2859 | /* |
| 2860 | * Check privileges. The three cases are task switch caused by... |
| 2861 | * |
| 2862 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2863 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2864 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2865 | */ |
| 2866 | if (reason == TASK_SWITCH_GATE) { |
| 2867 | if (idt_index != -1) { |
| 2868 | /* Software interrupts */ |
| 2869 | struct desc_struct task_gate_desc; |
| 2870 | int dpl; |
| 2871 | |
| 2872 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2873 | &task_gate_desc); |
| 2874 | if (ret != X86EMUL_CONTINUE) |
| 2875 | return ret; |
| 2876 | |
| 2877 | dpl = task_gate_desc.dpl; |
| 2878 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2879 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2880 | } |
| 2881 | } else if (reason != TASK_SWITCH_IRET) { |
| 2882 | int dpl = next_tss_desc.dpl; |
| 2883 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2884 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2885 | } |
| 2886 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2887 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2888 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2889 | if (!next_tss_desc.p || |
| 2890 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2891 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2892 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2893 | return X86EMUL_PROPAGATE_FAULT; |
| 2894 | } |
| 2895 | |
| 2896 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2897 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2898 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | if (reason == TASK_SWITCH_IRET) |
| 2902 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2903 | |
| 2904 | /* 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] | 2905 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2906 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2907 | old_tss_sel = 0xffff; |
| 2908 | |
| 2909 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2910 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2911 | old_tss_base, &next_tss_desc); |
| 2912 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2913 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2914 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2915 | if (ret != X86EMUL_CONTINUE) |
| 2916 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2917 | |
| 2918 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2919 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2920 | |
| 2921 | if (reason != TASK_SWITCH_IRET) { |
| 2922 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2923 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2926 | 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] | 2927 | 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] | 2928 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2929 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2930 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2931 | ctxt->lock_prefix = 0; |
| 2932 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2933 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2934 | } |
| 2935 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2936 | return ret; |
| 2937 | } |
| 2938 | |
| 2939 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2940 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2941 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2942 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2943 | int rc; |
| 2944 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2945 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2946 | ctxt->_eip = ctxt->eip; |
| 2947 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2948 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2949 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2950 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2951 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2952 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2953 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2954 | writeback_registers(ctxt); |
| 2955 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2956 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2957 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2960 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2961 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2962 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2963 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2964 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2965 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2966 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2967 | } |
| 2968 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2969 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2970 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2971 | u8 al, old_al; |
| 2972 | bool af, cf, old_cf; |
| 2973 | |
| 2974 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2975 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2976 | |
| 2977 | old_al = al; |
| 2978 | old_cf = cf; |
| 2979 | cf = false; |
| 2980 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2981 | if ((al & 0x0f) > 9 || af) { |
| 2982 | al -= 6; |
| 2983 | cf = old_cf | (al >= 250); |
| 2984 | af = true; |
| 2985 | } else { |
| 2986 | af = false; |
| 2987 | } |
| 2988 | if (old_al > 0x99 || old_cf) { |
| 2989 | al -= 0x60; |
| 2990 | cf = true; |
| 2991 | } |
| 2992 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2993 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2994 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2995 | ctxt->src.type = OP_IMM; |
| 2996 | ctxt->src.val = 0; |
| 2997 | ctxt->src.bytes = 1; |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 2998 | fastop(ctxt, em_or); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2999 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 3000 | if (cf) |
| 3001 | ctxt->eflags |= X86_EFLAGS_CF; |
| 3002 | if (af) |
| 3003 | ctxt->eflags |= X86_EFLAGS_AF; |
| 3004 | return X86EMUL_CONTINUE; |
| 3005 | } |
| 3006 | |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3007 | static int em_aam(struct x86_emulate_ctxt *ctxt) |
| 3008 | { |
| 3009 | u8 al, ah; |
| 3010 | |
| 3011 | if (ctxt->src.val == 0) |
| 3012 | return emulate_de(ctxt); |
| 3013 | |
| 3014 | al = ctxt->dst.val & 0xff; |
| 3015 | ah = al / ctxt->src.val; |
| 3016 | al %= ctxt->src.val; |
| 3017 | |
| 3018 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8); |
| 3019 | |
| 3020 | /* Set PF, ZF, SF */ |
| 3021 | ctxt->src.type = OP_IMM; |
| 3022 | ctxt->src.val = 0; |
| 3023 | ctxt->src.bytes = 1; |
| 3024 | fastop(ctxt, em_or); |
| 3025 | |
| 3026 | return X86EMUL_CONTINUE; |
| 3027 | } |
| 3028 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3029 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 3030 | { |
| 3031 | u8 al = ctxt->dst.val & 0xff; |
| 3032 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 3033 | |
| 3034 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 3035 | |
| 3036 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 3037 | |
Gleb Natapov | f583c29 | 2013-02-13 17:50:39 +0200 | [diff] [blame] | 3038 | /* Set PF, ZF, SF */ |
| 3039 | ctxt->src.type = OP_IMM; |
| 3040 | ctxt->src.val = 0; |
| 3041 | ctxt->src.bytes = 1; |
| 3042 | fastop(ctxt, em_or); |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3043 | |
| 3044 | return X86EMUL_CONTINUE; |
| 3045 | } |
| 3046 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3047 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 3048 | { |
| 3049 | long rel = ctxt->src.val; |
| 3050 | |
| 3051 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 3052 | jmp_rel(ctxt, rel); |
| 3053 | return em_push(ctxt); |
| 3054 | } |
| 3055 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3056 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3057 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3058 | u16 sel, old_cs; |
| 3059 | ulong old_eip; |
| 3060 | int rc; |
| 3061 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 3062 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3063 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3064 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3065 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3066 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3067 | return X86EMUL_CONTINUE; |
| 3068 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3069 | ctxt->_eip = 0; |
| 3070 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3071 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3072 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3073 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3074 | if (rc != X86EMUL_CONTINUE) |
| 3075 | return rc; |
| 3076 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3077 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3078 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3079 | } |
| 3080 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3081 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3082 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3083 | int rc; |
| 3084 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3085 | ctxt->dst.type = OP_REG; |
| 3086 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 3087 | ctxt->dst.bytes = ctxt->op_bytes; |
| 3088 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3089 | if (rc != X86EMUL_CONTINUE) |
| 3090 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3091 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3092 | return X86EMUL_CONTINUE; |
| 3093 | } |
| 3094 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3095 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3096 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3097 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3098 | ctxt->src.val = ctxt->dst.val; |
| 3099 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3100 | |
| 3101 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3102 | ctxt->dst.val = ctxt->src.orig_val; |
| 3103 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3104 | return X86EMUL_CONTINUE; |
| 3105 | } |
| 3106 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3107 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3108 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3109 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 3110 | return fastop(ctxt, em_imul); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3111 | } |
| 3112 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3113 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3114 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3115 | ctxt->dst.type = OP_REG; |
| 3116 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3117 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3118 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3119 | |
| 3120 | return X86EMUL_CONTINUE; |
| 3121 | } |
| 3122 | |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3123 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3124 | { |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3125 | u64 tsc = 0; |
| 3126 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3127 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3128 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3129 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3130 | return X86EMUL_CONTINUE; |
| 3131 | } |
| 3132 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3133 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3134 | { |
| 3135 | u64 pmc; |
| 3136 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3137 | 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] | 3138 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3139 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3140 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3141 | return X86EMUL_CONTINUE; |
| 3142 | } |
| 3143 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3144 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3145 | { |
Stefan Hajnoczi | 49597d8 | 2012-04-09 18:40:00 +0300 | [diff] [blame] | 3146 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3147 | return X86EMUL_CONTINUE; |
| 3148 | } |
| 3149 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3150 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3151 | { |
| 3152 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3153 | return emulate_gp(ctxt, 0); |
| 3154 | |
| 3155 | /* Disable writeback. */ |
| 3156 | ctxt->dst.type = OP_NONE; |
| 3157 | return X86EMUL_CONTINUE; |
| 3158 | } |
| 3159 | |
| 3160 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3161 | { |
| 3162 | unsigned long val; |
| 3163 | |
| 3164 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3165 | val = ctxt->src.val & ~0ULL; |
| 3166 | else |
| 3167 | val = ctxt->src.val & ~0U; |
| 3168 | |
| 3169 | /* #UD condition is already handled. */ |
| 3170 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3171 | return emulate_gp(ctxt, 0); |
| 3172 | |
| 3173 | /* Disable writeback. */ |
| 3174 | ctxt->dst.type = OP_NONE; |
| 3175 | return X86EMUL_CONTINUE; |
| 3176 | } |
| 3177 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3178 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3179 | { |
| 3180 | u64 msr_data; |
| 3181 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3182 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3183 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3184 | 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] | 3185 | return emulate_gp(ctxt, 0); |
| 3186 | |
| 3187 | return X86EMUL_CONTINUE; |
| 3188 | } |
| 3189 | |
| 3190 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3191 | { |
| 3192 | u64 msr_data; |
| 3193 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3194 | 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] | 3195 | return emulate_gp(ctxt, 0); |
| 3196 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3197 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3198 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3199 | return X86EMUL_CONTINUE; |
| 3200 | } |
| 3201 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3202 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3203 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3204 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3205 | return emulate_ud(ctxt); |
| 3206 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3207 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3208 | return X86EMUL_CONTINUE; |
| 3209 | } |
| 3210 | |
| 3211 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3212 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3213 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3214 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3215 | 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] | 3216 | return emulate_ud(ctxt); |
| 3217 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3218 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3219 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3220 | |
| 3221 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3222 | ctxt->dst.type = OP_NONE; |
| 3223 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3224 | } |
| 3225 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3226 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3227 | { |
| 3228 | u16 sel = ctxt->src.val; |
| 3229 | |
| 3230 | /* Disable writeback. */ |
| 3231 | ctxt->dst.type = OP_NONE; |
| 3232 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3233 | } |
| 3234 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3235 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3236 | { |
| 3237 | u16 sel = ctxt->src.val; |
| 3238 | |
| 3239 | /* Disable writeback. */ |
| 3240 | ctxt->dst.type = OP_NONE; |
| 3241 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3242 | } |
| 3243 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3244 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3245 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3246 | int rc; |
| 3247 | ulong linear; |
| 3248 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3249 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3250 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3251 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3252 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3253 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3254 | return X86EMUL_CONTINUE; |
| 3255 | } |
| 3256 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3257 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3258 | { |
| 3259 | ulong cr0; |
| 3260 | |
| 3261 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3262 | cr0 &= ~X86_CR0_TS; |
| 3263 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3264 | return X86EMUL_CONTINUE; |
| 3265 | } |
| 3266 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3267 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3268 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3269 | int rc; |
| 3270 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3271 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3272 | return X86EMUL_UNHANDLEABLE; |
| 3273 | |
| 3274 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3275 | if (rc != X86EMUL_CONTINUE) |
| 3276 | return rc; |
| 3277 | |
| 3278 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3279 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3280 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3281 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3282 | return X86EMUL_CONTINUE; |
| 3283 | } |
| 3284 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3285 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3286 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3287 | struct desc_ptr *ptr)) |
| 3288 | { |
| 3289 | struct desc_ptr desc_ptr; |
| 3290 | |
| 3291 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3292 | ctxt->op_bytes = 8; |
| 3293 | get(ctxt, &desc_ptr); |
| 3294 | if (ctxt->op_bytes == 2) { |
| 3295 | ctxt->op_bytes = 4; |
| 3296 | desc_ptr.address &= 0x00ffffff; |
| 3297 | } |
| 3298 | /* Disable writeback. */ |
| 3299 | ctxt->dst.type = OP_NONE; |
| 3300 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3301 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3302 | } |
| 3303 | |
| 3304 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3305 | { |
| 3306 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3307 | } |
| 3308 | |
| 3309 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3310 | { |
| 3311 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3312 | } |
| 3313 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3314 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3315 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3316 | struct desc_ptr desc_ptr; |
| 3317 | int rc; |
| 3318 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3319 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3320 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3321 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3322 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3323 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3324 | if (rc != X86EMUL_CONTINUE) |
| 3325 | return rc; |
| 3326 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3327 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3328 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3329 | return X86EMUL_CONTINUE; |
| 3330 | } |
| 3331 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3332 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3333 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3334 | int rc; |
| 3335 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3336 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3337 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3338 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3339 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3340 | return rc; |
| 3341 | } |
| 3342 | |
| 3343 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3344 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3345 | struct desc_ptr desc_ptr; |
| 3346 | int rc; |
| 3347 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3348 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3349 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3350 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3351 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3352 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3353 | if (rc != X86EMUL_CONTINUE) |
| 3354 | return rc; |
| 3355 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3356 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3357 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3358 | return X86EMUL_CONTINUE; |
| 3359 | } |
| 3360 | |
| 3361 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3362 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3363 | ctxt->dst.bytes = 2; |
| 3364 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3365 | return X86EMUL_CONTINUE; |
| 3366 | } |
| 3367 | |
| 3368 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3369 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3370 | 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] | 3371 | | (ctxt->src.val & 0x0f)); |
| 3372 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3373 | return X86EMUL_CONTINUE; |
| 3374 | } |
| 3375 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3376 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3377 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3378 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3379 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3380 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3381 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3382 | |
| 3383 | return X86EMUL_CONTINUE; |
| 3384 | } |
| 3385 | |
| 3386 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3387 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3388 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3389 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3390 | |
| 3391 | return X86EMUL_CONTINUE; |
| 3392 | } |
| 3393 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3394 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3395 | { |
| 3396 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3397 | &ctxt->dst.val)) |
| 3398 | return X86EMUL_IO_NEEDED; |
| 3399 | |
| 3400 | return X86EMUL_CONTINUE; |
| 3401 | } |
| 3402 | |
| 3403 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3404 | { |
| 3405 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3406 | &ctxt->src.val, 1); |
| 3407 | /* Disable writeback. */ |
| 3408 | ctxt->dst.type = OP_NONE; |
| 3409 | return X86EMUL_CONTINUE; |
| 3410 | } |
| 3411 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3412 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3413 | { |
| 3414 | if (emulator_bad_iopl(ctxt)) |
| 3415 | return emulate_gp(ctxt, 0); |
| 3416 | |
| 3417 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3418 | return X86EMUL_CONTINUE; |
| 3419 | } |
| 3420 | |
| 3421 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3422 | { |
| 3423 | if (emulator_bad_iopl(ctxt)) |
| 3424 | return emulate_gp(ctxt, 0); |
| 3425 | |
| 3426 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3427 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3428 | return X86EMUL_CONTINUE; |
| 3429 | } |
| 3430 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3431 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3432 | { |
| 3433 | u32 eax, ebx, ecx, edx; |
| 3434 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3435 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3436 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3437 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3438 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3439 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3440 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3441 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3442 | return X86EMUL_CONTINUE; |
| 3443 | } |
| 3444 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3445 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3446 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3447 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3448 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3449 | return X86EMUL_CONTINUE; |
| 3450 | } |
| 3451 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3452 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3453 | { |
| 3454 | switch (ctxt->op_bytes) { |
| 3455 | #ifdef CONFIG_X86_64 |
| 3456 | case 8: |
| 3457 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3458 | break; |
| 3459 | #endif |
| 3460 | default: |
| 3461 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3462 | break; |
| 3463 | } |
| 3464 | return X86EMUL_CONTINUE; |
| 3465 | } |
| 3466 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3467 | static bool valid_cr(int nr) |
| 3468 | { |
| 3469 | switch (nr) { |
| 3470 | case 0: |
| 3471 | case 2 ... 4: |
| 3472 | case 8: |
| 3473 | return true; |
| 3474 | default: |
| 3475 | return false; |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3480 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3481 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3482 | return emulate_ud(ctxt); |
| 3483 | |
| 3484 | return X86EMUL_CONTINUE; |
| 3485 | } |
| 3486 | |
| 3487 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3488 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3489 | u64 new_val = ctxt->src.val64; |
| 3490 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3491 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3492 | |
| 3493 | static u64 cr_reserved_bits[] = { |
| 3494 | 0xffffffff00000000ULL, |
| 3495 | 0, 0, 0, /* CR3 checked later */ |
| 3496 | CR4_RESERVED_BITS, |
| 3497 | 0, 0, 0, |
| 3498 | CR8_RESERVED_BITS, |
| 3499 | }; |
| 3500 | |
| 3501 | if (!valid_cr(cr)) |
| 3502 | return emulate_ud(ctxt); |
| 3503 | |
| 3504 | if (new_val & cr_reserved_bits[cr]) |
| 3505 | return emulate_gp(ctxt, 0); |
| 3506 | |
| 3507 | switch (cr) { |
| 3508 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3509 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3510 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3511 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3512 | return emulate_gp(ctxt, 0); |
| 3513 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3514 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3515 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3516 | |
| 3517 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3518 | !(cr4 & X86_CR4_PAE)) |
| 3519 | return emulate_gp(ctxt, 0); |
| 3520 | |
| 3521 | break; |
| 3522 | } |
| 3523 | case 3: { |
| 3524 | u64 rsvd = 0; |
| 3525 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3526 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3527 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3528 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3529 | else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3530 | rsvd = CR3_PAE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3531 | else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3532 | rsvd = CR3_NONPAE_RESERVED_BITS; |
| 3533 | |
| 3534 | if (new_val & rsvd) |
| 3535 | return emulate_gp(ctxt, 0); |
| 3536 | |
| 3537 | break; |
| 3538 | } |
| 3539 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3540 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3541 | |
| 3542 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3543 | return emulate_gp(ctxt, 0); |
| 3544 | |
| 3545 | break; |
| 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | return X86EMUL_CONTINUE; |
| 3550 | } |
| 3551 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3552 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3553 | { |
| 3554 | unsigned long dr7; |
| 3555 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3556 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3557 | |
| 3558 | /* Check if DR7.Global_Enable is set */ |
| 3559 | return dr7 & (1 << 13); |
| 3560 | } |
| 3561 | |
| 3562 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3563 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3564 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3565 | u64 cr4; |
| 3566 | |
| 3567 | if (dr > 7) |
| 3568 | return emulate_ud(ctxt); |
| 3569 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3570 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3571 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3572 | return emulate_ud(ctxt); |
| 3573 | |
| 3574 | if (check_dr7_gd(ctxt)) |
| 3575 | return emulate_db(ctxt); |
| 3576 | |
| 3577 | return X86EMUL_CONTINUE; |
| 3578 | } |
| 3579 | |
| 3580 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3581 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3582 | u64 new_val = ctxt->src.val64; |
| 3583 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3584 | |
| 3585 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3586 | return emulate_gp(ctxt, 0); |
| 3587 | |
| 3588 | return check_dr_read(ctxt); |
| 3589 | } |
| 3590 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3591 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3592 | { |
| 3593 | u64 efer; |
| 3594 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3595 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3596 | |
| 3597 | if (!(efer & EFER_SVME)) |
| 3598 | return emulate_ud(ctxt); |
| 3599 | |
| 3600 | return X86EMUL_CONTINUE; |
| 3601 | } |
| 3602 | |
| 3603 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3604 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3605 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3606 | |
| 3607 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3608 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3609 | return emulate_gp(ctxt, 0); |
| 3610 | |
| 3611 | return check_svme(ctxt); |
| 3612 | } |
| 3613 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3614 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3615 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3616 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3617 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3618 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3619 | return emulate_ud(ctxt); |
| 3620 | |
| 3621 | return X86EMUL_CONTINUE; |
| 3622 | } |
| 3623 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3624 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3625 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3626 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3627 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3628 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3629 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3630 | (rcx > 3)) |
| 3631 | return emulate_gp(ctxt, 0); |
| 3632 | |
| 3633 | return X86EMUL_CONTINUE; |
| 3634 | } |
| 3635 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3636 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3637 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3638 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3639 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3640 | return emulate_gp(ctxt, 0); |
| 3641 | |
| 3642 | return X86EMUL_CONTINUE; |
| 3643 | } |
| 3644 | |
| 3645 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3646 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3647 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3648 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3649 | return emulate_gp(ctxt, 0); |
| 3650 | |
| 3651 | return X86EMUL_CONTINUE; |
| 3652 | } |
| 3653 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3654 | #define D(_y) { .flags = (_y) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3655 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3656 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
| 3657 | .check_perm = (_p) } |
Gleb Natapov | 0b789ee | 2013-04-11 11:59:55 +0300 | [diff] [blame] | 3658 | #define N D(NotImpl) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3659 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3660 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3661 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3662 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3663 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3664 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3665 | #define II(_f, _e, _i) \ |
| 3666 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3667 | #define IIP(_f, _e, _i, _p) \ |
| 3668 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ |
| 3669 | .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3670 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3671 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3672 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3673 | #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] | 3674 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3675 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3676 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3677 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3678 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3679 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3680 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3681 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3682 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3683 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3684 | DI(SrcNone | Priv, monitor), |
| 3685 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3686 | N, N, N, N, N, N, |
| 3687 | }; |
| 3688 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3689 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3690 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
| 3691 | II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall), |
| 3692 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3693 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3694 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3695 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3696 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3697 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3698 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3699 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3700 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3701 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3702 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3703 | N, N, N, N, N, N, |
| 3704 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3705 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3706 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3707 | F(Lock, em_add), |
| 3708 | F(Lock | PageTable, em_or), |
| 3709 | F(Lock, em_adc), |
| 3710 | F(Lock, em_sbb), |
| 3711 | F(Lock | PageTable, em_and), |
| 3712 | F(Lock, em_sub), |
| 3713 | F(Lock, em_xor), |
| 3714 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3715 | }; |
| 3716 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3717 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3718 | 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] | 3719 | }; |
| 3720 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3721 | static const struct opcode group2[] = { |
| 3722 | F(DstMem | ModRM, em_rol), |
| 3723 | F(DstMem | ModRM, em_ror), |
| 3724 | F(DstMem | ModRM, em_rcl), |
| 3725 | F(DstMem | ModRM, em_rcr), |
| 3726 | F(DstMem | ModRM, em_shl), |
| 3727 | F(DstMem | ModRM, em_shr), |
| 3728 | F(DstMem | ModRM, em_shl), |
| 3729 | F(DstMem | ModRM, em_sar), |
| 3730 | }; |
| 3731 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3732 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3733 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3734 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3735 | F(DstMem | SrcNone | Lock, em_not), |
| 3736 | F(DstMem | SrcNone | Lock, em_neg), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3737 | I(SrcMem, em_mul_ex), |
| 3738 | I(SrcMem, em_imul_ex), |
| 3739 | I(SrcMem, em_div_ex), |
| 3740 | I(SrcMem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3741 | }; |
| 3742 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3743 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3744 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3745 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3746 | N, N, N, N, N, N, |
| 3747 | }; |
| 3748 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3749 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3750 | F(DstMem | SrcNone | Lock, em_inc), |
| 3751 | F(DstMem | SrcNone | Lock, em_dec), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3752 | I(SrcMem | Stack, em_grp45), |
| 3753 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3754 | I(SrcMem | Stack, em_grp45), |
| 3755 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
Gleb Natapov | 188424b | 2013-04-11 12:32:14 +0300 | [diff] [blame] | 3756 | I(SrcMem | Stack, em_grp45), D(Undefined), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3757 | }; |
| 3758 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3759 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3760 | DI(Prot, sldt), |
| 3761 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3762 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3763 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3764 | N, N, N, N, |
| 3765 | }; |
| 3766 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3767 | static const struct group_dual group7 = { { |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3768 | II(Mov | DstMem | Priv, em_sgdt, sgdt), |
| 3769 | II(Mov | DstMem | Priv, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3770 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3771 | II(SrcMem | Priv, em_lidt, lidt), |
| 3772 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3773 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3774 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3775 | }, { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3776 | I(SrcNone | Priv | VendorSpecific, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3777 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3778 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3779 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3780 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3781 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3782 | } }; |
| 3783 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3784 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3785 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 3786 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3787 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3788 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3789 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3790 | }; |
| 3791 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3792 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3793 | 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] | 3794 | }, { |
| 3795 | N, N, N, N, N, N, N, N, |
| 3796 | } }; |
| 3797 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3798 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3799 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3800 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3801 | }; |
| 3802 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3803 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3804 | 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] | 3805 | }; |
| 3806 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3807 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3808 | I(0, em_mov), N, N, N, |
| 3809 | }; |
| 3810 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3811 | static const struct escape escape_d9 = { { |
| 3812 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3813 | }, { |
| 3814 | /* 0xC0 - 0xC7 */ |
| 3815 | N, N, N, N, N, N, N, N, |
| 3816 | /* 0xC8 - 0xCF */ |
| 3817 | N, N, N, N, N, N, N, N, |
| 3818 | /* 0xD0 - 0xC7 */ |
| 3819 | N, N, N, N, N, N, N, N, |
| 3820 | /* 0xD8 - 0xDF */ |
| 3821 | N, N, N, N, N, N, N, N, |
| 3822 | /* 0xE0 - 0xE7 */ |
| 3823 | N, N, N, N, N, N, N, N, |
| 3824 | /* 0xE8 - 0xEF */ |
| 3825 | N, N, N, N, N, N, N, N, |
| 3826 | /* 0xF0 - 0xF7 */ |
| 3827 | N, N, N, N, N, N, N, N, |
| 3828 | /* 0xF8 - 0xFF */ |
| 3829 | N, N, N, N, N, N, N, N, |
| 3830 | } }; |
| 3831 | |
| 3832 | static const struct escape escape_db = { { |
| 3833 | N, N, N, N, N, N, N, N, |
| 3834 | }, { |
| 3835 | /* 0xC0 - 0xC7 */ |
| 3836 | N, N, N, N, N, N, N, N, |
| 3837 | /* 0xC8 - 0xCF */ |
| 3838 | N, N, N, N, N, N, N, N, |
| 3839 | /* 0xD0 - 0xC7 */ |
| 3840 | N, N, N, N, N, N, N, N, |
| 3841 | /* 0xD8 - 0xDF */ |
| 3842 | N, N, N, N, N, N, N, N, |
| 3843 | /* 0xE0 - 0xE7 */ |
| 3844 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3845 | /* 0xE8 - 0xEF */ |
| 3846 | N, N, N, N, N, N, N, N, |
| 3847 | /* 0xF0 - 0xF7 */ |
| 3848 | N, N, N, N, N, N, N, N, |
| 3849 | /* 0xF8 - 0xFF */ |
| 3850 | N, N, N, N, N, N, N, N, |
| 3851 | } }; |
| 3852 | |
| 3853 | static const struct escape escape_dd = { { |
| 3854 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3855 | }, { |
| 3856 | /* 0xC0 - 0xC7 */ |
| 3857 | N, N, N, N, N, N, N, N, |
| 3858 | /* 0xC8 - 0xCF */ |
| 3859 | N, N, N, N, N, N, N, N, |
| 3860 | /* 0xD0 - 0xC7 */ |
| 3861 | N, N, N, N, N, N, N, N, |
| 3862 | /* 0xD8 - 0xDF */ |
| 3863 | N, N, N, N, N, N, N, N, |
| 3864 | /* 0xE0 - 0xE7 */ |
| 3865 | N, N, N, N, N, N, N, N, |
| 3866 | /* 0xE8 - 0xEF */ |
| 3867 | N, N, N, N, N, N, N, N, |
| 3868 | /* 0xF0 - 0xF7 */ |
| 3869 | N, N, N, N, N, N, N, N, |
| 3870 | /* 0xF8 - 0xFF */ |
| 3871 | N, N, N, N, N, N, N, N, |
| 3872 | } }; |
| 3873 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3874 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3875 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3876 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3877 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3878 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3879 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3880 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3881 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3882 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3883 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3884 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3885 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3886 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3887 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3888 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3889 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3890 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3891 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3892 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3893 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3894 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3895 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3896 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3897 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3898 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3899 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3900 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3901 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3902 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3903 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3904 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3905 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3906 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3907 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3908 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3909 | N, N, N, N, |
| 3910 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3911 | I(SrcImm | Mov | Stack, em_push), |
| 3912 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3913 | I(SrcImmByte | Mov | Stack, em_push), |
| 3914 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3915 | 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] | 3916 | 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] | 3917 | /* 0x70 - 0x7F */ |
| 3918 | X16(D(SrcImmByte)), |
| 3919 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3920 | G(ByteOp | DstMem | SrcImm, group1), |
| 3921 | G(DstMem | SrcImm, group1), |
| 3922 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3923 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3924 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3925 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3926 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3927 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3928 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3929 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3930 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3931 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3932 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3933 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3934 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3935 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3936 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3937 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3938 | II(ImplicitOps | Stack, em_pushf, pushf), |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3939 | II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3940 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3941 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3942 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3943 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3944 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3945 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3946 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3947 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3948 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3949 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3950 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3951 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3952 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3953 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3954 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3955 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3956 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3957 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3958 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3959 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3960 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3961 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3962 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
| 3963 | N, I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3964 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3965 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3966 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3967 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 3968 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Paolo Bonzini | a035d5c6 | 2013-05-09 11:32:49 +0200 | [diff] [blame] | 3969 | I(DstAcc | SrcImmUByte | No64, em_aam), |
Paolo Bonzini | 326f578 | 2013-05-09 11:32:51 +0200 | [diff] [blame] | 3970 | I(DstAcc | SrcImmUByte | No64, em_aad), |
| 3971 | F(DstAcc | ByteOp | No64, em_salc), |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 3972 | I(DstAcc | SrcXLat | ByteOp, em_mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3973 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3974 | 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] | 3975 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3976 | X3(I(SrcImmByte, em_loop)), |
| 3977 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3978 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3979 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3980 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3981 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3982 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3983 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3984 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3985 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3986 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3987 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3988 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3989 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3990 | D(ImplicitOps), D(ImplicitOps), |
| 3991 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3992 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3993 | }; |
| 3994 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3995 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3996 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3997 | G(0, group6), GD(0, &group7), N, N, |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3998 | N, I(ImplicitOps | VendorSpecific, em_syscall), |
| 3999 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 4000 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4001 | N, D(ImplicitOps | ModRM), N, N, |
| 4002 | /* 0x10 - 0x1F */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4003 | N, N, N, N, N, N, N, N, |
| 4004 | D(ImplicitOps | ModRM), N, N, N, N, N, N, D(ImplicitOps | ModRM), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4005 | /* 0x20 - 0x2F */ |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4006 | DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4007 | DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read), |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 4008 | IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write), |
| 4009 | IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4010 | N, N, N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 4011 | N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
| 4012 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4013 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4014 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4015 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 4016 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 4017 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4018 | I(ImplicitOps | VendorSpecific, em_sysenter), |
| 4019 | I(ImplicitOps | Priv | VendorSpecific, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4020 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4021 | N, N, N, N, N, N, N, N, |
| 4022 | /* 0x40 - 0x4F */ |
| 4023 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 4024 | /* 0x50 - 0x5F */ |
| 4025 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4026 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4027 | N, N, N, N, |
| 4028 | N, N, N, N, |
| 4029 | N, N, N, N, |
| 4030 | 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] | 4031 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4032 | N, N, N, N, |
| 4033 | N, N, N, N, |
| 4034 | N, N, N, N, |
| 4035 | 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] | 4036 | /* 0x80 - 0x8F */ |
| 4037 | X16(D(SrcImm)), |
| 4038 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4039 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4040 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4041 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4042 | II(ImplicitOps, em_cpuid, cpuid), |
| 4043 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4044 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4045 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4046 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4047 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4048 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4049 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4050 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4051 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Avi Kivity | 4d75834 | 2013-01-19 19:51:55 +0200 | [diff] [blame] | 4052 | D(ModRM), F(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4053 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 4054 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4055 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4056 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4057 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4058 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4059 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4060 | /* 0xB8 - 0xBF */ |
| 4061 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4062 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame] | 4063 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 4064 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4065 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4066 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 4067 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4068 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4069 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4070 | /* 0xC8 - 0xCF */ |
| 4071 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4072 | /* 0xD0 - 0xDF */ |
| 4073 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4074 | /* 0xE0 - 0xEF */ |
| 4075 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4076 | /* 0xF0 - 0xFF */ |
| 4077 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4078 | }; |
| 4079 | |
| 4080 | #undef D |
| 4081 | #undef N |
| 4082 | #undef G |
| 4083 | #undef GD |
| 4084 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4085 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4086 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4087 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4088 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4089 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4090 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4091 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4092 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4093 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4094 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4095 | { |
| 4096 | unsigned size; |
| 4097 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4098 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4099 | if (size == 8) |
| 4100 | size = 4; |
| 4101 | return size; |
| 4102 | } |
| 4103 | |
| 4104 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4105 | unsigned size, bool sign_extension) |
| 4106 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4107 | int rc = X86EMUL_CONTINUE; |
| 4108 | |
| 4109 | op->type = OP_IMM; |
| 4110 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4111 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4112 | /* NB. Immediates are sign-extended as necessary. */ |
| 4113 | switch (op->bytes) { |
| 4114 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4115 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4116 | break; |
| 4117 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4118 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4119 | break; |
| 4120 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4121 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4122 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4123 | case 8: |
| 4124 | op->val = insn_fetch(s64, ctxt); |
| 4125 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4126 | } |
| 4127 | if (!sign_extension) { |
| 4128 | switch (op->bytes) { |
| 4129 | case 1: |
| 4130 | op->val &= 0xff; |
| 4131 | break; |
| 4132 | case 2: |
| 4133 | op->val &= 0xffff; |
| 4134 | break; |
| 4135 | case 4: |
| 4136 | op->val &= 0xffffffff; |
| 4137 | break; |
| 4138 | } |
| 4139 | } |
| 4140 | done: |
| 4141 | return rc; |
| 4142 | } |
| 4143 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4144 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4145 | unsigned d) |
| 4146 | { |
| 4147 | int rc = X86EMUL_CONTINUE; |
| 4148 | |
| 4149 | switch (d) { |
| 4150 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4151 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4152 | break; |
| 4153 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4154 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4155 | break; |
| 4156 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4157 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4158 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4159 | *op = ctxt->memop; |
| 4160 | ctxt->memopp = op; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4161 | if ((ctxt->d & BitOp) && op == &ctxt->dst) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4162 | fetch_bit_operand(ctxt); |
| 4163 | op->orig_val = op->val; |
| 4164 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4165 | case OpMem64: |
| 4166 | ctxt->memop.bytes = 8; |
| 4167 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4168 | case OpAcc: |
| 4169 | op->type = OP_REG; |
| 4170 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4171 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4172 | fetch_register_operand(op); |
| 4173 | op->orig_val = op->val; |
| 4174 | break; |
| 4175 | case OpDI: |
| 4176 | op->type = OP_MEM; |
| 4177 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4178 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4179 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4180 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4181 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4182 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4183 | break; |
| 4184 | case OpDX: |
| 4185 | op->type = OP_REG; |
| 4186 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4187 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4188 | fetch_register_operand(op); |
| 4189 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4190 | case OpCL: |
| 4191 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4192 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4193 | break; |
| 4194 | case OpImmByte: |
| 4195 | rc = decode_imm(ctxt, op, 1, true); |
| 4196 | break; |
| 4197 | case OpOne: |
| 4198 | op->bytes = 1; |
| 4199 | op->val = 1; |
| 4200 | break; |
| 4201 | case OpImm: |
| 4202 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4203 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4204 | case OpImm64: |
| 4205 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4206 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4207 | case OpMem8: |
| 4208 | ctxt->memop.bytes = 1; |
Gleb Natapov | 660696d | 2013-04-24 13:38:36 +0300 | [diff] [blame] | 4209 | if (ctxt->memop.type == OP_REG) { |
| 4210 | ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1); |
| 4211 | fetch_register_operand(&ctxt->memop); |
| 4212 | } |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4213 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4214 | case OpMem16: |
| 4215 | ctxt->memop.bytes = 2; |
| 4216 | goto mem_common; |
| 4217 | case OpMem32: |
| 4218 | ctxt->memop.bytes = 4; |
| 4219 | goto mem_common; |
| 4220 | case OpImmU16: |
| 4221 | rc = decode_imm(ctxt, op, 2, false); |
| 4222 | break; |
| 4223 | case OpImmU: |
| 4224 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4225 | break; |
| 4226 | case OpSI: |
| 4227 | op->type = OP_MEM; |
| 4228 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4229 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4230 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4231 | op->addr.mem.seg = seg_override(ctxt); |
| 4232 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4233 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4234 | break; |
Paolo Bonzini | 7fa5795 | 2013-05-09 11:32:50 +0200 | [diff] [blame] | 4235 | case OpXLat: |
| 4236 | op->type = OP_MEM; |
| 4237 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4238 | op->addr.mem.ea = |
| 4239 | register_address(ctxt, |
| 4240 | reg_read(ctxt, VCPU_REGS_RBX) + |
| 4241 | (reg_read(ctxt, VCPU_REGS_RAX) & 0xff)); |
| 4242 | op->addr.mem.seg = seg_override(ctxt); |
| 4243 | op->val = 0; |
| 4244 | break; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4245 | case OpImmFAddr: |
| 4246 | op->type = OP_IMM; |
| 4247 | op->addr.mem.ea = ctxt->_eip; |
| 4248 | op->bytes = ctxt->op_bytes + 2; |
| 4249 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4250 | break; |
| 4251 | case OpMemFAddr: |
| 4252 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4253 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4254 | case OpES: |
| 4255 | op->val = VCPU_SREG_ES; |
| 4256 | break; |
| 4257 | case OpCS: |
| 4258 | op->val = VCPU_SREG_CS; |
| 4259 | break; |
| 4260 | case OpSS: |
| 4261 | op->val = VCPU_SREG_SS; |
| 4262 | break; |
| 4263 | case OpDS: |
| 4264 | op->val = VCPU_SREG_DS; |
| 4265 | break; |
| 4266 | case OpFS: |
| 4267 | op->val = VCPU_SREG_FS; |
| 4268 | break; |
| 4269 | case OpGS: |
| 4270 | op->val = VCPU_SREG_GS; |
| 4271 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4272 | case OpImplicit: |
| 4273 | /* Special instructions do their own operand decoding. */ |
| 4274 | default: |
| 4275 | op->type = OP_NONE; /* Disable writeback. */ |
| 4276 | break; |
| 4277 | } |
| 4278 | |
| 4279 | done: |
| 4280 | return rc; |
| 4281 | } |
| 4282 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4283 | 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] | 4284 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4285 | int rc = X86EMUL_CONTINUE; |
| 4286 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4287 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4288 | bool op_prefix = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4289 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4290 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4291 | ctxt->memop.type = OP_NONE; |
| 4292 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4293 | ctxt->_eip = ctxt->eip; |
| 4294 | ctxt->fetch.start = ctxt->_eip; |
| 4295 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4296 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4297 | memcpy(ctxt->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4298 | |
| 4299 | switch (mode) { |
| 4300 | case X86EMUL_MODE_REAL: |
| 4301 | case X86EMUL_MODE_VM86: |
| 4302 | case X86EMUL_MODE_PROT16: |
| 4303 | def_op_bytes = def_ad_bytes = 2; |
| 4304 | break; |
| 4305 | case X86EMUL_MODE_PROT32: |
| 4306 | def_op_bytes = def_ad_bytes = 4; |
| 4307 | break; |
| 4308 | #ifdef CONFIG_X86_64 |
| 4309 | case X86EMUL_MODE_PROT64: |
| 4310 | def_op_bytes = 4; |
| 4311 | def_ad_bytes = 8; |
| 4312 | break; |
| 4313 | #endif |
| 4314 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4315 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4316 | } |
| 4317 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4318 | ctxt->op_bytes = def_op_bytes; |
| 4319 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4320 | |
| 4321 | /* Legacy prefixes. */ |
| 4322 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4323 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4324 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4325 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4326 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4327 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4328 | break; |
| 4329 | case 0x67: /* address-size override */ |
| 4330 | if (mode == X86EMUL_MODE_PROT64) |
| 4331 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4332 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4333 | else |
| 4334 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4335 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4336 | break; |
| 4337 | case 0x26: /* ES override */ |
| 4338 | case 0x2e: /* CS override */ |
| 4339 | case 0x36: /* SS override */ |
| 4340 | case 0x3e: /* DS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4341 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4342 | break; |
| 4343 | case 0x64: /* FS override */ |
| 4344 | case 0x65: /* GS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4345 | set_seg_override(ctxt, ctxt->b & 7); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4346 | break; |
| 4347 | case 0x40 ... 0x4f: /* REX */ |
| 4348 | if (mode != X86EMUL_MODE_PROT64) |
| 4349 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4350 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4351 | continue; |
| 4352 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4353 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4354 | break; |
| 4355 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4356 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4357 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4358 | break; |
| 4359 | default: |
| 4360 | goto done_prefixes; |
| 4361 | } |
| 4362 | |
| 4363 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4364 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4365 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | done_prefixes: |
| 4369 | |
| 4370 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4371 | if (ctxt->rex_prefix & 8) |
| 4372 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4373 | |
| 4374 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4375 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4376 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4377 | if (ctxt->b == 0x0f) { |
| 4378 | ctxt->twobyte = 1; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4379 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4380 | opcode = twobyte_table[ctxt->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4381 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4382 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4383 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4384 | if (ctxt->d & ModRM) |
| 4385 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4386 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4387 | while (ctxt->d & GroupMask) { |
| 4388 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4389 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4390 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4391 | opcode = opcode.u.group[goffset]; |
| 4392 | break; |
| 4393 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4394 | goffset = (ctxt->modrm >> 3) & 7; |
| 4395 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4396 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4397 | else |
| 4398 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4399 | break; |
| 4400 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4401 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4402 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4403 | break; |
| 4404 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4405 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4406 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4407 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4408 | switch (simd_prefix) { |
| 4409 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4410 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4411 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4412 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4413 | } |
| 4414 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4415 | case Escape: |
| 4416 | if (ctxt->modrm > 0xbf) |
| 4417 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4418 | else |
| 4419 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4420 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4421 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4422 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4423 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4424 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4425 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4426 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4427 | } |
| 4428 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4429 | ctxt->execute = opcode.u.execute; |
| 4430 | ctxt->check_perm = opcode.check_perm; |
| 4431 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4432 | |
| 4433 | /* Unrecognised? */ |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4434 | if (ctxt->d == 0 || (ctxt->d & NotImpl)) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4435 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4436 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4437 | if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4438 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4439 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4440 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
| 4441 | ctxt->op_bytes = 8; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4442 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4443 | if (ctxt->d & Op3264) { |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4444 | if (mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4445 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4446 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4447 | ctxt->op_bytes = 4; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4448 | } |
| 4449 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4450 | if (ctxt->d & Sse) |
| 4451 | ctxt->op_bytes = 16; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4452 | else if (ctxt->d & Mmx) |
| 4453 | ctxt->op_bytes = 8; |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4454 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4455 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4456 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4457 | rc = decode_modrm(ctxt, &ctxt->memop); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4458 | if (!ctxt->has_seg_override) |
| 4459 | set_seg_override(ctxt, ctxt->modrm_seg); |
| 4460 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4461 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4462 | if (rc != X86EMUL_CONTINUE) |
| 4463 | goto done; |
| 4464 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4465 | if (!ctxt->has_seg_override) |
| 4466 | set_seg_override(ctxt, VCPU_SREG_DS); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4467 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4468 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4469 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4470 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
| 4471 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4472 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4473 | /* |
| 4474 | * Decode and fetch the source operand: register, memory |
| 4475 | * or immediate. |
| 4476 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4477 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4478 | if (rc != X86EMUL_CONTINUE) |
| 4479 | goto done; |
| 4480 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4481 | /* |
| 4482 | * Decode and fetch the second source operand: register, memory |
| 4483 | * or immediate. |
| 4484 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4485 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4486 | if (rc != X86EMUL_CONTINUE) |
| 4487 | goto done; |
| 4488 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4489 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4490 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4491 | |
| 4492 | done: |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4493 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
| 4494 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4495 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4496 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4497 | } |
| 4498 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4499 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4500 | { |
| 4501 | return ctxt->d & PageTable; |
| 4502 | } |
| 4503 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4504 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4505 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4506 | /* The second termination condition only applies for REPE |
| 4507 | * and REPNE. Test if the repeat string operation prefix is |
| 4508 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4509 | * corresponding termination condition according to: |
| 4510 | * - if REPE/REPZ and ZF = 0 then done |
| 4511 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4512 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4513 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4514 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4515 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4516 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4517 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4518 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4519 | return true; |
| 4520 | |
| 4521 | return false; |
| 4522 | } |
| 4523 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4524 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4525 | { |
| 4526 | bool fault = false; |
| 4527 | |
| 4528 | ctxt->ops->get_fpu(ctxt); |
| 4529 | asm volatile("1: fwait \n\t" |
| 4530 | "2: \n\t" |
| 4531 | ".pushsection .fixup,\"ax\" \n\t" |
| 4532 | "3: \n\t" |
| 4533 | "movb $1, %[fault] \n\t" |
| 4534 | "jmp 2b \n\t" |
| 4535 | ".popsection \n\t" |
| 4536 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4537 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4538 | ctxt->ops->put_fpu(ctxt); |
| 4539 | |
| 4540 | if (unlikely(fault)) |
| 4541 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4542 | |
| 4543 | return X86EMUL_CONTINUE; |
| 4544 | } |
| 4545 | |
| 4546 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4547 | struct operand *op) |
| 4548 | { |
| 4549 | if (op->type == OP_MM) |
| 4550 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4551 | } |
| 4552 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4553 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4554 | { |
| 4555 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
| 4556 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
| 4557 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
| 4558 | : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags) |
| 4559 | : "c"(ctxt->src2.val), [fastop]"S"(fop)); |
| 4560 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
| 4561 | return X86EMUL_CONTINUE; |
| 4562 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4563 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4564 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4565 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4566 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4567 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4568 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4569 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4570 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4571 | |
Gleb Natapov | 1146a78 | 2013-04-11 12:30:01 +0300 | [diff] [blame] | 4572 | if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) || |
| 4573 | (ctxt->d & Undefined)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4574 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 4575 | goto done; |
| 4576 | } |
| 4577 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4578 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4579 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4580 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4581 | goto done; |
| 4582 | } |
| 4583 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4584 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4585 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4586 | goto done; |
| 4587 | } |
| 4588 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4589 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4590 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
Avi Kivity | 1253791d | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4591 | rc = emulate_ud(ctxt); |
| 4592 | goto done; |
| 4593 | } |
| 4594 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4595 | 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] | 4596 | rc = emulate_nm(ctxt); |
| 4597 | goto done; |
| 4598 | } |
| 4599 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4600 | if (ctxt->d & Mmx) { |
| 4601 | rc = flush_pending_x87_faults(ctxt); |
| 4602 | if (rc != X86EMUL_CONTINUE) |
| 4603 | goto done; |
| 4604 | /* |
| 4605 | * Now that we know the fpu is exception safe, we can fetch |
| 4606 | * operands from it. |
| 4607 | */ |
| 4608 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4609 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4610 | if (!(ctxt->d & Mov)) |
| 4611 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4612 | } |
| 4613 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4614 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4615 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4616 | X86_ICPT_PRE_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4617 | if (rc != X86EMUL_CONTINUE) |
| 4618 | goto done; |
| 4619 | } |
| 4620 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4621 | /* Privileged instruction can be executed only in CPL=0 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4622 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4623 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4624 | goto done; |
| 4625 | } |
| 4626 | |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4627 | /* Instruction can only be executed in protected mode */ |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 4628 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4629 | rc = emulate_ud(ctxt); |
| 4630 | goto done; |
| 4631 | } |
| 4632 | |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4633 | /* Do instruction specific permission checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4634 | if (ctxt->check_perm) { |
| 4635 | rc = ctxt->check_perm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4636 | if (rc != X86EMUL_CONTINUE) |
| 4637 | goto done; |
| 4638 | } |
| 4639 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4640 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4641 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4642 | X86_ICPT_POST_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4643 | if (rc != X86EMUL_CONTINUE) |
| 4644 | goto done; |
| 4645 | } |
| 4646 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4647 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4648 | /* All REP prefixes have the same first termination condition */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4649 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4650 | ctxt->eip = ctxt->_eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4651 | goto done; |
| 4652 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4653 | } |
| 4654 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4655 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4656 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4657 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4658 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4659 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4660 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4661 | } |
| 4662 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4663 | if (ctxt->src2.type == OP_MEM) { |
| 4664 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4665 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4666 | if (rc != X86EMUL_CONTINUE) |
| 4667 | goto done; |
| 4668 | } |
| 4669 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4670 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4671 | goto special_insn; |
| 4672 | |
| 4673 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4674 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4675 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4676 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4677 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4678 | if (rc != X86EMUL_CONTINUE) |
| 4679 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4680 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4681 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4682 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4683 | special_insn: |
| 4684 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4685 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4686 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4687 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4688 | if (rc != X86EMUL_CONTINUE) |
| 4689 | goto done; |
| 4690 | } |
| 4691 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4692 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4693 | if (ctxt->d & Fastop) { |
| 4694 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4695 | rc = fastop(ctxt, fop); |
| 4696 | if (rc != X86EMUL_CONTINUE) |
| 4697 | goto done; |
| 4698 | goto writeback; |
| 4699 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4700 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4701 | if (rc != X86EMUL_CONTINUE) |
| 4702 | goto done; |
| 4703 | goto writeback; |
| 4704 | } |
| 4705 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4706 | if (ctxt->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4707 | goto twobyte_insn; |
| 4708 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4709 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4710 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4711 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4712 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4713 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4714 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4715 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4716 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4717 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4718 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4719 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4720 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4721 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4722 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4723 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 4724 | break; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4725 | rc = em_xchg(ctxt); |
| 4726 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4727 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4728 | switch (ctxt->op_bytes) { |
| 4729 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4730 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4731 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4732 | } |
| 4733 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4734 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4735 | rc = emulate_int(ctxt, 3); |
| 4736 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4737 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4738 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4739 | break; |
| 4740 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4741 | if (ctxt->eflags & EFLG_OF) |
| 4742 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4743 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4744 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4745 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4746 | jmp_rel(ctxt, ctxt->src.val); |
| 4747 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4748 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4749 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4750 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4751 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4752 | case 0xf5: /* cmc */ |
| 4753 | /* complement carry flag from eflags reg */ |
| 4754 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4755 | break; |
| 4756 | case 0xf8: /* clc */ |
| 4757 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4758 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4759 | case 0xf9: /* stc */ |
| 4760 | ctxt->eflags |= EFLG_CF; |
| 4761 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4762 | case 0xfc: /* cld */ |
| 4763 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4764 | break; |
| 4765 | case 0xfd: /* std */ |
| 4766 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4767 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4768 | default: |
| 4769 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4770 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4771 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4772 | if (rc != X86EMUL_CONTINUE) |
| 4773 | goto done; |
| 4774 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4775 | writeback: |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 4776 | rc = writeback(ctxt); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4777 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4778 | goto done; |
| 4779 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4780 | /* |
| 4781 | * restore dst type in case the decoding will be reused |
| 4782 | * (happens for string instruction ) |
| 4783 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4784 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4785 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4786 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4787 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4788 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4789 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4790 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4791 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4792 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4793 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4794 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4795 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4796 | count = ctxt->src.count; |
| 4797 | else |
| 4798 | count = ctxt->dst.count; |
| 4799 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4800 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4801 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4802 | if (!string_insn_completed(ctxt)) { |
| 4803 | /* |
| 4804 | * Re-enter guest when pio read ahead buffer is empty |
| 4805 | * or, if it is not used, after each 1024 iteration. |
| 4806 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4807 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4808 | (r->end == 0 || r->end != r->pos)) { |
| 4809 | /* |
| 4810 | * Reset read cache. Usually happens before |
| 4811 | * decode, but since instruction is restarted |
| 4812 | * we have to do it here. |
| 4813 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4814 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4815 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4816 | return EMULATION_RESTART; |
| 4817 | } |
| 4818 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4819 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4820 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4821 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4822 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4823 | |
| 4824 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4825 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4826 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4827 | if (rc == X86EMUL_INTERCEPTED) |
| 4828 | return EMULATION_INTERCEPTED; |
| 4829 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4830 | if (rc == X86EMUL_CONTINUE) |
| 4831 | writeback_registers(ctxt); |
| 4832 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4833 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4834 | |
| 4835 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4836 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4837 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4838 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4839 | break; |
| 4840 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4841 | case 0x0d: /* GrpP (prefetch) */ |
| 4842 | case 0x18: /* Grp16 (prefetch/nop) */ |
Paolo Bonzini | 103f98e | 2013-05-30 13:22:39 +0200 | [diff] [blame] | 4843 | case 0x1f: /* nop */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4844 | break; |
| 4845 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4846 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4847 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4848 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4849 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4850 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4851 | case 0x40 ... 0x4f: /* cmov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4852 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
| 4853 | if (!test_cc(ctxt->b, ctxt->eflags)) |
| 4854 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4855 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4856 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4857 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4858 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4859 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4860 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4861 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4862 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4863 | case 0xae: /* clflush */ |
| 4864 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4865 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4866 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4867 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4868 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4869 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4870 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4871 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4872 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4873 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4874 | break; |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4875 | case 0xc0 ... 0xc1: /* xadd */ |
Avi Kivity | 158de57 | 2013-01-19 19:51:57 +0200 | [diff] [blame] | 4876 | fastop(ctxt, em_add); |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4877 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4878 | ctxt->src.val = ctxt->dst.orig_val; |
| 4879 | write_register_operand(&ctxt->src); |
Wei Yongjun | 92f738a5 | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4880 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4881 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4882 | ctxt->dst.bytes = ctxt->op_bytes; |
| 4883 | ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val : |
| 4884 | (u64) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4885 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4886 | default: |
| 4887 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4888 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4889 | |
| 4890 | if (rc != X86EMUL_CONTINUE) |
| 4891 | goto done; |
| 4892 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4893 | goto writeback; |
| 4894 | |
| 4895 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4896 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4897 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4898 | |
| 4899 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4900 | { |
| 4901 | invalidate_registers(ctxt); |
| 4902 | } |
| 4903 | |
| 4904 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4905 | { |
| 4906 | writeback_registers(ctxt); |
| 4907 | } |