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 */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 63 | |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 64 | #define OpBits 5 /* Width of operand field */ |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 65 | #define OpMask ((1ull << OpBits) - 1) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 66 | |
| 67 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 68 | * Opcode effective-address decode tables. |
| 69 | * Note that we only emulate instructions that have at least one memory |
| 70 | * operand (excluding implicit stack references). We assume that stack |
| 71 | * references and instruction fetches will never occur in special memory |
| 72 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 73 | * not be handled. |
| 74 | */ |
| 75 | |
| 76 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | ab85b12b | 2010-07-29 15:11:49 +0300 | [diff] [blame] | 77 | #define ByteOp (1<<0) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 78 | /* Destination operand type. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 79 | #define DstShift 1 |
| 80 | #define ImplicitOps (OpImplicit << DstShift) |
| 81 | #define DstReg (OpReg << DstShift) |
| 82 | #define DstMem (OpMem << DstShift) |
| 83 | #define DstAcc (OpAcc << DstShift) |
| 84 | #define DstDI (OpDI << DstShift) |
| 85 | #define DstMem64 (OpMem64 << DstShift) |
| 86 | #define DstImmUByte (OpImmUByte << DstShift) |
| 87 | #define DstDX (OpDX << DstShift) |
| 88 | #define DstMask (OpMask << DstShift) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 89 | /* Source operand type. */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 90 | #define SrcShift 6 |
| 91 | #define SrcNone (OpNone << SrcShift) |
| 92 | #define SrcReg (OpReg << SrcShift) |
| 93 | #define SrcMem (OpMem << SrcShift) |
| 94 | #define SrcMem16 (OpMem16 << SrcShift) |
| 95 | #define SrcMem32 (OpMem32 << SrcShift) |
| 96 | #define SrcImm (OpImm << SrcShift) |
| 97 | #define SrcImmByte (OpImmByte << SrcShift) |
| 98 | #define SrcOne (OpOne << SrcShift) |
| 99 | #define SrcImmUByte (OpImmUByte << SrcShift) |
| 100 | #define SrcImmU (OpImmU << SrcShift) |
| 101 | #define SrcSI (OpSI << SrcShift) |
| 102 | #define SrcImmFAddr (OpImmFAddr << SrcShift) |
| 103 | #define SrcMemFAddr (OpMemFAddr << SrcShift) |
| 104 | #define SrcAcc (OpAcc << SrcShift) |
| 105 | #define SrcImmU16 (OpImmU16 << SrcShift) |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 106 | #define SrcImm64 (OpImm64 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 107 | #define SrcDX (OpDX << SrcShift) |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 108 | #define SrcMem8 (OpMem8 << SrcShift) |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 109 | #define SrcMask (OpMask << SrcShift) |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 110 | #define BitOp (1<<11) |
| 111 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ |
| 112 | #define String (1<<13) /* String instruction (rep capable) */ |
| 113 | #define Stack (1<<14) /* Stack instruction (push/pop) */ |
| 114 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ |
| 115 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ |
| 116 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ |
| 117 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ |
| 118 | #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] | 119 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
Marcelo Tosatti | 221192b | 2011-05-30 15:23:14 -0300 | [diff] [blame] | 120 | #define Sse (1<<18) /* SSE Vector instruction */ |
Avi Kivity | 20c29ff | 2011-09-13 10:45:44 +0300 | [diff] [blame] | 121 | /* Generic ModRM decode. */ |
| 122 | #define ModRM (1<<19) |
| 123 | /* Destination is only written; never read. */ |
| 124 | #define Mov (1<<20) |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 125 | /* Misc flags */ |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 126 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 127 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
Avi Kivity | 5a506b1 | 2010-08-01 15:10:29 +0300 | [diff] [blame] | 128 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 129 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 130 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 131 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 132 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 133 | #define No64 (1<<28) |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 134 | #define PageTable (1 << 29) /* instruction used to write page table */ |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 135 | /* Source 2 operand type */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 136 | #define Src2Shift (30) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 137 | #define Src2None (OpNone << Src2Shift) |
| 138 | #define Src2CL (OpCL << Src2Shift) |
| 139 | #define Src2ImmByte (OpImmByte << Src2Shift) |
| 140 | #define Src2One (OpOne << Src2Shift) |
| 141 | #define Src2Imm (OpImm << Src2Shift) |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 142 | #define Src2ES (OpES << Src2Shift) |
| 143 | #define Src2CS (OpCS << Src2Shift) |
| 144 | #define Src2SS (OpSS << Src2Shift) |
| 145 | #define Src2DS (OpDS << Src2Shift) |
| 146 | #define Src2FS (OpFS << Src2Shift) |
| 147 | #define Src2GS (OpGS << Src2Shift) |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 148 | #define Src2Mask (OpMask << Src2Shift) |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 149 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 150 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
| 151 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ |
| 152 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 153 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 154 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 155 | |
Avi Kivity | d0e5332 | 2010-07-29 15:11:54 +0300 | [diff] [blame] | 156 | #define X2(x...) x, x |
| 157 | #define X3(x...) X2(x), x |
| 158 | #define X4(x...) X2(x), X2(x) |
| 159 | #define X5(x...) X4(x), x |
| 160 | #define X6(x...) X4(x), X2(x) |
| 161 | #define X7(x...) X4(x), X3(x) |
| 162 | #define X8(x...) X4(x), X4(x) |
| 163 | #define X16(x...) X8(x), X8(x) |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 164 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 165 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
| 166 | #define FASTOP_SIZE 8 |
| 167 | |
| 168 | /* |
| 169 | * fastop functions have a special calling convention: |
| 170 | * |
| 171 | * dst: [rdx]:rax (in/out) |
| 172 | * src: rbx (in/out) |
| 173 | * src2: rcx (in) |
| 174 | * flags: rflags (in/out) |
| 175 | * |
| 176 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for |
| 177 | * different operand sizes can be reached by calculation, rather than a jump |
| 178 | * table (which would be bigger than the code). |
| 179 | * |
| 180 | * fastop functions are declared as taking a never-defined fastop parameter, |
| 181 | * so they can't be called from C directly. |
| 182 | */ |
| 183 | |
| 184 | struct fastop; |
| 185 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 186 | struct opcode { |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 187 | u64 flags : 56; |
| 188 | u64 intercept : 8; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 189 | union { |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 190 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 191 | const struct opcode *group; |
| 192 | const struct group_dual *gdual; |
| 193 | const struct gprefix *gprefix; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 194 | const struct escape *esc; |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 195 | void (*fastop)(struct fastop *fake); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 196 | } u; |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 197 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | struct group_dual { |
| 201 | struct opcode mod012[8]; |
| 202 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 203 | }; |
| 204 | |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 205 | struct gprefix { |
| 206 | struct opcode pfx_no; |
| 207 | struct opcode pfx_66; |
| 208 | struct opcode pfx_f2; |
| 209 | struct opcode pfx_f3; |
| 210 | }; |
| 211 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 212 | struct escape { |
| 213 | struct opcode op[8]; |
| 214 | struct opcode high[64]; |
| 215 | }; |
| 216 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 217 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 218 | #define EFLG_ID (1<<21) |
| 219 | #define EFLG_VIP (1<<20) |
| 220 | #define EFLG_VIF (1<<19) |
| 221 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 222 | #define EFLG_VM (1<<17) |
| 223 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 224 | #define EFLG_IOPL (3<<12) |
| 225 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 226 | #define EFLG_OF (1<<11) |
| 227 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 228 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 229 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 230 | #define EFLG_SF (1<<7) |
| 231 | #define EFLG_ZF (1<<6) |
| 232 | #define EFLG_AF (1<<4) |
| 233 | #define EFLG_PF (1<<2) |
| 234 | #define EFLG_CF (1<<0) |
| 235 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 236 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 237 | #define EFLG_RESERVED_ONE_MASK 2 |
| 238 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 239 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 240 | { |
| 241 | if (!(ctxt->regs_valid & (1 << nr))) { |
| 242 | ctxt->regs_valid |= 1 << nr; |
| 243 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); |
| 244 | } |
| 245 | return ctxt->_regs[nr]; |
| 246 | } |
| 247 | |
| 248 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 249 | { |
| 250 | ctxt->regs_valid |= 1 << nr; |
| 251 | ctxt->regs_dirty |= 1 << nr; |
| 252 | return &ctxt->_regs[nr]; |
| 253 | } |
| 254 | |
| 255 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) |
| 256 | { |
| 257 | reg_read(ctxt, nr); |
| 258 | return reg_write(ctxt, nr); |
| 259 | } |
| 260 | |
| 261 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) |
| 262 | { |
| 263 | unsigned reg; |
| 264 | |
| 265 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) |
| 266 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); |
| 267 | } |
| 268 | |
| 269 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) |
| 270 | { |
| 271 | ctxt->regs_dirty = 0; |
| 272 | ctxt->regs_valid = 0; |
| 273 | } |
| 274 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 275 | /* |
| 276 | * Instruction emulation: |
| 277 | * Most instructions are emulated directly via a fragment of inline assembly |
| 278 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 279 | * any modified flags. |
| 280 | */ |
| 281 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 282 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 283 | #define _LO32 "k" /* force 32-bit operand */ |
| 284 | #define _STK "%%rsp" /* stack pointer */ |
| 285 | #elif defined(__i386__) |
| 286 | #define _LO32 "" /* force 32-bit operand */ |
| 287 | #define _STK "%%esp" /* stack pointer */ |
| 288 | #endif |
| 289 | |
| 290 | /* |
| 291 | * These EFLAGS bits are restored from saved value during emulation, and |
| 292 | * any changes are written back to the saved value after emulation. |
| 293 | */ |
| 294 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 295 | |
| 296 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 297 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 298 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 299 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 300 | "push %"_tmp"; " \ |
| 301 | "push %"_tmp"; " \ |
| 302 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 303 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 304 | "pushf; " \ |
| 305 | "notl %"_LO32 _tmp"; " \ |
| 306 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 307 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 308 | "pop %"_tmp"; " \ |
| 309 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 310 | "popf; " \ |
| 311 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 312 | |
| 313 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 314 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 315 | /* _sav |= EFLAGS & _msk; */ \ |
| 316 | "pushf; " \ |
| 317 | "pop %"_tmp"; " \ |
| 318 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 319 | "orl %"_LO32 _tmp",%"_sav"; " |
| 320 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 321 | #ifdef CONFIG_X86_64 |
| 322 | #define ON64(x) x |
| 323 | #else |
| 324 | #define ON64(x) |
| 325 | #endif |
| 326 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 327 | #define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 328 | do { \ |
| 329 | __asm__ __volatile__ ( \ |
| 330 | _PRE_EFLAGS("0", "4", "2") \ |
| 331 | _op _suffix " %"_x"3,%1; " \ |
| 332 | _POST_EFLAGS("0", "4", "2") \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 333 | : "=m" ((ctxt)->eflags), \ |
| 334 | "+q" (*(_dsttype*)&(ctxt)->dst.val), \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 335 | "=&r" (_tmp) \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 336 | : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 337 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 338 | |
| 339 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 340 | /* Raw emulation: instruction has two explicit operands. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 341 | #define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 342 | do { \ |
| 343 | unsigned long _tmp; \ |
| 344 | \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 345 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 346 | case 2: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 347 | ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 348 | break; \ |
| 349 | case 4: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 350 | ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 351 | break; \ |
| 352 | case 8: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 353 | ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 354 | break; \ |
| 355 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 356 | } while (0) |
| 357 | |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 358 | #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] | 359 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 360 | unsigned long _tmp; \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 361 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 362 | case 1: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 363 | ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 364 | break; \ |
| 365 | default: \ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 366 | __emulate_2op_nobyte(ctxt, _op, \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 367 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 368 | break; \ |
| 369 | } \ |
| 370 | } while (0) |
| 371 | |
| 372 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 373 | #define emulate_2op_SrcB(ctxt, _op) \ |
| 374 | __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | |
| 376 | /* Source operand is byte, word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 377 | #define emulate_2op_SrcV(ctxt, _op) \ |
| 378 | __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 379 | |
| 380 | /* Source operand is word, long or quad sized. */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 381 | #define emulate_2op_SrcV_nobyte(ctxt, _op) \ |
| 382 | __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r") |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 383 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 384 | /* Instruction has three operands and one operand is stored in ECX register */ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 385 | #define __emulate_2op_cl(ctxt, _op, _suffix, _type) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 386 | do { \ |
| 387 | unsigned long _tmp; \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 388 | _type _clv = (ctxt)->src2.val; \ |
| 389 | _type _srcv = (ctxt)->src.val; \ |
| 390 | _type _dstv = (ctxt)->dst.val; \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 391 | \ |
| 392 | __asm__ __volatile__ ( \ |
| 393 | _PRE_EFLAGS("0", "5", "2") \ |
| 394 | _op _suffix " %4,%1 \n" \ |
| 395 | _POST_EFLAGS("0", "5", "2") \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 396 | : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 397 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 398 | ); \ |
| 399 | \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 400 | (ctxt)->src2.val = (unsigned long) _clv; \ |
| 401 | (ctxt)->src2.val = (unsigned long) _srcv; \ |
| 402 | (ctxt)->dst.val = (unsigned long) _dstv; \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 403 | } while (0) |
| 404 | |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 405 | #define emulate_2op_cl(ctxt, _op) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 406 | do { \ |
Avi Kivity | 761441b | 2011-09-07 16:41:36 +0300 | [diff] [blame] | 407 | switch ((ctxt)->dst.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 408 | case 2: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 409 | __emulate_2op_cl(ctxt, _op, "w", u16); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 410 | break; \ |
| 411 | case 4: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 412 | __emulate_2op_cl(ctxt, _op, "l", u32); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 413 | break; \ |
| 414 | case 8: \ |
Avi Kivity | 29053a6 | 2011-09-07 16:41:37 +0300 | [diff] [blame] | 415 | ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 416 | break; \ |
| 417 | } \ |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 418 | } while (0) |
| 419 | |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 420 | #define __emulate_1op(ctxt, _op, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 421 | do { \ |
| 422 | unsigned long _tmp; \ |
| 423 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 424 | __asm__ __volatile__ ( \ |
| 425 | _PRE_EFLAGS("0", "3", "2") \ |
| 426 | _op _suffix " %1; " \ |
| 427 | _POST_EFLAGS("0", "3", "2") \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 428 | : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 429 | "=&r" (_tmp) \ |
| 430 | : "i" (EFLAGS_MASK)); \ |
| 431 | } while (0) |
| 432 | |
| 433 | /* Instruction has only one explicit operand (no source operand). */ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 434 | #define emulate_1op(ctxt, _op) \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 435 | do { \ |
Avi Kivity | d1eef45 | 2011-09-07 16:41:38 +0300 | [diff] [blame] | 436 | switch ((ctxt)->dst.bytes) { \ |
| 437 | case 1: __emulate_1op(ctxt, _op, "b"); break; \ |
| 438 | case 2: __emulate_1op(ctxt, _op, "w"); break; \ |
| 439 | case 4: __emulate_1op(ctxt, _op, "l"); break; \ |
| 440 | case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 441 | } \ |
| 442 | } while (0) |
| 443 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 444 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
| 445 | #define FOP_RET "ret \n\t" |
| 446 | |
| 447 | #define FOP_START(op) \ |
| 448 | extern void em_##op(struct fastop *fake); \ |
| 449 | asm(".pushsection .text, \"ax\" \n\t" \ |
| 450 | ".global em_" #op " \n\t" \ |
| 451 | FOP_ALIGN \ |
| 452 | "em_" #op ": \n\t" |
| 453 | |
| 454 | #define FOP_END \ |
| 455 | ".popsection") |
| 456 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 457 | #define FOPNOP() FOP_ALIGN FOP_RET |
| 458 | |
Avi Kivity | b7d491e | 2013-01-04 16:18:49 +0200 | [diff] [blame] | 459 | #define FOP1E(op, dst) \ |
| 460 | FOP_ALIGN #op " %" #dst " \n\t" FOP_RET |
| 461 | |
| 462 | #define FASTOP1(op) \ |
| 463 | FOP_START(op) \ |
| 464 | FOP1E(op##b, al) \ |
| 465 | FOP1E(op##w, ax) \ |
| 466 | FOP1E(op##l, eax) \ |
| 467 | ON64(FOP1E(op##q, rax)) \ |
| 468 | FOP_END |
| 469 | |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 470 | #define FOP2E(op, dst, src) \ |
| 471 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET |
| 472 | |
| 473 | #define FASTOP2(op) \ |
| 474 | FOP_START(op) \ |
| 475 | FOP2E(op##b, al, bl) \ |
| 476 | FOP2E(op##w, ax, bx) \ |
| 477 | FOP2E(op##l, eax, ebx) \ |
| 478 | ON64(FOP2E(op##q, rax, rbx)) \ |
| 479 | FOP_END |
| 480 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 481 | /* 2 operand, word only */ |
| 482 | #define FASTOP2W(op) \ |
| 483 | FOP_START(op) \ |
| 484 | FOPNOP() \ |
| 485 | FOP2E(op##w, ax, bx) \ |
| 486 | FOP2E(op##l, eax, ebx) \ |
| 487 | ON64(FOP2E(op##q, rax, rbx)) \ |
| 488 | FOP_END |
| 489 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 490 | /* 2 operand, src is CL */ |
| 491 | #define FASTOP2CL(op) \ |
| 492 | FOP_START(op) \ |
| 493 | FOP2E(op##b, al, cl) \ |
| 494 | FOP2E(op##w, ax, cl) \ |
| 495 | FOP2E(op##l, eax, cl) \ |
| 496 | ON64(FOP2E(op##q, rax, cl)) \ |
| 497 | FOP_END |
| 498 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 499 | #define FOP3E(op, dst, src, src2) \ |
| 500 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET |
| 501 | |
| 502 | /* 3-operand, word-only, src2=cl */ |
| 503 | #define FASTOP3WCL(op) \ |
| 504 | FOP_START(op) \ |
| 505 | FOPNOP() \ |
| 506 | FOP3E(op##w, ax, bx, cl) \ |
| 507 | FOP3E(op##l, eax, ebx, cl) \ |
| 508 | ON64(FOP3E(op##q, rax, rbx, cl)) \ |
| 509 | FOP_END |
| 510 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 511 | /* Special case for SETcc - 1 instruction per cc */ |
| 512 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" |
| 513 | |
| 514 | FOP_START(setcc) |
| 515 | FOP_SETCC(seto) |
| 516 | FOP_SETCC(setno) |
| 517 | FOP_SETCC(setc) |
| 518 | FOP_SETCC(setnc) |
| 519 | FOP_SETCC(setz) |
| 520 | FOP_SETCC(setnz) |
| 521 | FOP_SETCC(setbe) |
| 522 | FOP_SETCC(setnbe) |
| 523 | FOP_SETCC(sets) |
| 524 | FOP_SETCC(setns) |
| 525 | FOP_SETCC(setp) |
| 526 | FOP_SETCC(setnp) |
| 527 | FOP_SETCC(setl) |
| 528 | FOP_SETCC(setnl) |
| 529 | FOP_SETCC(setle) |
| 530 | FOP_SETCC(setnle) |
| 531 | FOP_END; |
| 532 | |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 533 | #define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 534 | do { \ |
| 535 | unsigned long _tmp; \ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 536 | ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \ |
| 537 | ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 538 | \ |
| 539 | __asm__ __volatile__ ( \ |
| 540 | _PRE_EFLAGS("0", "5", "1") \ |
| 541 | "1: \n\t" \ |
| 542 | _op _suffix " %6; " \ |
| 543 | "2: \n\t" \ |
| 544 | _POST_EFLAGS("0", "5", "1") \ |
| 545 | ".pushsection .fixup,\"ax\" \n\t" \ |
| 546 | "3: movb $1, %4 \n\t" \ |
| 547 | "jmp 2b \n\t" \ |
| 548 | ".popsection \n\t" \ |
| 549 | _ASM_EXTABLE(1b, 3b) \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 550 | : "=m" ((ctxt)->eflags), "=&r" (_tmp), \ |
| 551 | "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \ |
H. Peter Anvin | cb7cb28 | 2012-11-21 14:41:21 -0800 | [diff] [blame] | 552 | : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 553 | } while (0) |
| 554 | |
Mohammed Gamal | 3f9f53b | 2010-08-08 21:11:37 +0300 | [diff] [blame] | 555 | /* 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] | 556 | #define emulate_1op_rax_rdx(ctxt, _op, _ex) \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 557 | do { \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 558 | switch((ctxt)->src.bytes) { \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 559 | case 1: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 560 | __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 561 | break; \ |
| 562 | case 2: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 563 | __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \ |
Avi Kivity | 7295261 | 2011-04-20 13:12:27 +0300 | [diff] [blame] | 564 | break; \ |
| 565 | case 4: \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 566 | __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 567 | break; \ |
| 568 | case 8: ON64( \ |
Avi Kivity | e8f2b1d | 2011-09-07 16:41:40 +0300 | [diff] [blame] | 569 | __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \ |
Avi Kivity | f6b3597 | 2010-08-26 11:59:00 +0300 | [diff] [blame] | 570 | break; \ |
| 571 | } \ |
| 572 | } while (0) |
| 573 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 574 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
| 575 | enum x86_intercept intercept, |
| 576 | enum x86_intercept_stage stage) |
| 577 | { |
| 578 | struct x86_instruction_info info = { |
| 579 | .intercept = intercept, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 580 | .rep_prefix = ctxt->rep_prefix, |
| 581 | .modrm_mod = ctxt->modrm_mod, |
| 582 | .modrm_reg = ctxt->modrm_reg, |
| 583 | .modrm_rm = ctxt->modrm_rm, |
| 584 | .src_val = ctxt->src.val64, |
| 585 | .src_bytes = ctxt->src.bytes, |
| 586 | .dst_bytes = ctxt->dst.bytes, |
| 587 | .ad_bytes = ctxt->ad_bytes, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 588 | .next_rip = ctxt->eip, |
| 589 | }; |
| 590 | |
Avi Kivity | 2953538 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 591 | return ctxt->ops->intercept(ctxt, &info, stage); |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 594 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
| 595 | { |
| 596 | *dest = (*dest & ~mask) | (src & mask); |
| 597 | } |
| 598 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 599 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 600 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 601 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 604 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
| 605 | { |
| 606 | u16 sel; |
| 607 | struct desc_struct ss; |
| 608 | |
| 609 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 610 | return ~0UL; |
| 611 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); |
| 612 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ |
| 613 | } |
| 614 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 615 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
| 616 | { |
| 617 | return (__fls(stack_mask(ctxt)) + 1) >> 3; |
| 618 | } |
| 619 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 620 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 621 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 622 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 623 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 624 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 625 | return reg; |
| 626 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 627 | return reg & ad_mask(ctxt); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static inline unsigned long |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 631 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 632 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 633 | return address_mask(ctxt, reg); |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 636 | static void masked_increment(ulong *reg, ulong mask, int inc) |
| 637 | { |
| 638 | assign_masked(reg, *reg + inc, mask); |
| 639 | } |
| 640 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 641 | static inline void |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 642 | 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] | 643 | { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 644 | ulong mask; |
| 645 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 646 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 647 | mask = ~0UL; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 648 | else |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 649 | mask = ad_mask(ctxt); |
| 650 | masked_increment(reg, mask, inc); |
| 651 | } |
| 652 | |
| 653 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) |
| 654 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 655 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 656 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 657 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 658 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 659 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 660 | register_address_increment(ctxt, &ctxt->_eip, rel); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 661 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 662 | |
Avi Kivity | 5669768 | 2011-04-03 14:08:51 +0300 | [diff] [blame] | 663 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 664 | { |
| 665 | u32 limit = get_desc_limit(desc); |
| 666 | |
| 667 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 668 | } |
| 669 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 670 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 671 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 672 | ctxt->has_seg_override = true; |
| 673 | ctxt->seg_override = seg; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 674 | } |
| 675 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 676 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 677 | { |
| 678 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 679 | return 0; |
| 680 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 681 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 682 | } |
| 683 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 684 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 685 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 686 | if (!ctxt->has_seg_override) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 687 | return 0; |
| 688 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 689 | return ctxt->seg_override; |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 690 | } |
| 691 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 692 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 693 | u32 error, bool valid) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 694 | { |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 695 | ctxt->exception.vector = vec; |
| 696 | ctxt->exception.error_code = error; |
| 697 | ctxt->exception.error_code_valid = valid; |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 698 | return X86EMUL_PROPAGATE_FAULT; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 699 | } |
| 700 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 701 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
| 702 | { |
| 703 | return emulate_exception(ctxt, DB_VECTOR, 0, false); |
| 704 | } |
| 705 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 706 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 707 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 708 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 709 | } |
| 710 | |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 711 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
| 712 | { |
| 713 | return emulate_exception(ctxt, SS_VECTOR, err, true); |
| 714 | } |
| 715 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 716 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 717 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 718 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 719 | } |
| 720 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 721 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 722 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 723 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 724 | } |
| 725 | |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 726 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
| 727 | { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 728 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 729 | } |
| 730 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 731 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
| 732 | { |
| 733 | return emulate_exception(ctxt, NM_VECTOR, 0, false); |
| 734 | } |
| 735 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 736 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
| 737 | { |
| 738 | u16 selector; |
| 739 | struct desc_struct desc; |
| 740 | |
| 741 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); |
| 742 | return selector; |
| 743 | } |
| 744 | |
| 745 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 746 | unsigned seg) |
| 747 | { |
| 748 | u16 dummy; |
| 749 | u32 base3; |
| 750 | struct desc_struct desc; |
| 751 | |
| 752 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); |
| 753 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); |
| 754 | } |
| 755 | |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 756 | /* |
| 757 | * x86 defines three classes of vector instructions: explicitly |
| 758 | * aligned, explicitly unaligned, and the rest, which change behaviour |
| 759 | * depending on whether they're AVX encoded or not. |
| 760 | * |
| 761 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is |
| 762 | * subject to the same check. |
| 763 | */ |
| 764 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) |
| 765 | { |
| 766 | if (likely(size < 16)) |
| 767 | return false; |
| 768 | |
| 769 | if (ctxt->d & Aligned) |
| 770 | return true; |
| 771 | else if (ctxt->d & Unaligned) |
| 772 | return false; |
| 773 | else if (ctxt->d & Avx) |
| 774 | return false; |
| 775 | else |
| 776 | return true; |
| 777 | } |
| 778 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 779 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 780 | struct segmented_address addr, |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 781 | unsigned size, bool write, bool fetch, |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 782 | ulong *linear) |
| 783 | { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 784 | struct desc_struct desc; |
| 785 | bool usable; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 786 | ulong la; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 787 | u32 lim; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 788 | u16 sel; |
Gleb Natapov | 3a78a4f | 2012-12-20 16:57:42 +0200 | [diff] [blame] | 789 | unsigned cpl; |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 790 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 791 | la = seg_base(ctxt, addr.seg) + addr.ea; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 792 | switch (ctxt->mode) { |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 793 | case X86EMUL_MODE_PROT64: |
| 794 | if (((signed long)la << 16) >> 16 != la) |
| 795 | return emulate_gp(ctxt, 0); |
| 796 | break; |
| 797 | default: |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 798 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
| 799 | addr.seg); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 800 | if (!usable) |
| 801 | goto bad; |
Gleb Natapov | 58b7825 | 2012-12-11 15:14:12 +0200 | [diff] [blame] | 802 | /* code segment in protected mode or read-only data segment */ |
| 803 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) |
| 804 | || !(desc.type & 2)) && write) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 805 | goto bad; |
| 806 | /* unreadable code segment */ |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 807 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 808 | goto bad; |
| 809 | lim = desc_limit_scaled(&desc); |
| 810 | if ((desc.type & 8) || !(desc.type & 4)) { |
| 811 | /* expand-up segment */ |
| 812 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 813 | goto bad; |
| 814 | } else { |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 815 | /* expand-down segment */ |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 816 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
| 817 | goto bad; |
| 818 | lim = desc.d ? 0xffffffff : 0xffff; |
| 819 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) |
| 820 | goto bad; |
| 821 | } |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 822 | cpl = ctxt->ops->cpl(ctxt); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 823 | if (!(desc.type & 8)) { |
| 824 | /* data segment */ |
| 825 | if (cpl > desc.dpl) |
| 826 | goto bad; |
| 827 | } else if ((desc.type & 8) && !(desc.type & 4)) { |
| 828 | /* nonconforming code segment */ |
| 829 | if (cpl != desc.dpl) |
| 830 | goto bad; |
| 831 | } else if ((desc.type & 8) && (desc.type & 4)) { |
| 832 | /* conforming code segment */ |
| 833 | if (cpl < desc.dpl) |
| 834 | goto bad; |
| 835 | } |
| 836 | break; |
| 837 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 838 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 839 | la &= (u32)-1; |
Avi Kivity | 1c11b37 | 2012-04-09 18:39:59 +0300 | [diff] [blame] | 840 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
| 841 | return emulate_gp(ctxt, 0); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 842 | *linear = la; |
| 843 | return X86EMUL_CONTINUE; |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 844 | bad: |
| 845 | if (addr.seg == VCPU_SREG_SS) |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 846 | return emulate_ss(ctxt, sel); |
Avi Kivity | 618ff15 | 2011-04-03 12:32:09 +0300 | [diff] [blame] | 847 | else |
Avi Kivity | 0afbe2f | 2012-08-21 17:07:06 +0300 | [diff] [blame] | 848 | return emulate_gp(ctxt, sel); |
Avi Kivity | 52fd8b4 | 2011-04-03 12:33:12 +0300 | [diff] [blame] | 849 | } |
| 850 | |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 851 | static int linearize(struct x86_emulate_ctxt *ctxt, |
| 852 | struct segmented_address addr, |
| 853 | unsigned size, bool write, |
| 854 | ulong *linear) |
| 855 | { |
| 856 | return __linearize(ctxt, addr, size, write, false, linear); |
| 857 | } |
| 858 | |
| 859 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 860 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
| 861 | struct segmented_address addr, |
| 862 | void *data, |
| 863 | unsigned size) |
| 864 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 865 | int rc; |
| 866 | ulong linear; |
| 867 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 868 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 869 | if (rc != X86EMUL_CONTINUE) |
| 870 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 871 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 872 | } |
| 873 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 874 | /* |
| 875 | * Fetch the next byte of the instruction being emulated which is pointed to |
| 876 | * by ctxt->_eip, then increment ctxt->_eip. |
| 877 | * |
| 878 | * Also prefetch the remaining bytes of the instruction without crossing page |
| 879 | * boundary if they are not in fetch_cache yet. |
| 880 | */ |
| 881 | 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] | 882 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 883 | struct fetch_cache *fc = &ctxt->fetch; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 884 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 885 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 886 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 887 | if (ctxt->_eip == fc->end) { |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 888 | unsigned long linear; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 889 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
| 890 | .ea = ctxt->_eip }; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 891 | cur_size = fc->end - fc->start; |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 892 | size = min(15UL - cur_size, |
| 893 | PAGE_SIZE - offset_in_page(ctxt->_eip)); |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 894 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 895 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Nelson Elhage | 3d9b938 | 2011-04-18 12:05:53 -0400 | [diff] [blame] | 896 | return rc; |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 897 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
| 898 | size, &ctxt->exception); |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 899 | if (unlikely(rc != X86EMUL_CONTINUE)) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 900 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 901 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 902 | } |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 903 | *dest = fc->data[ctxt->_eip - fc->start]; |
| 904 | ctxt->_eip++; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 905 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 909 | void *dest, unsigned size) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 910 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 911 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 912 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 913 | /* x86 instructions are limited to 15 bytes. */ |
Takuya Yoshikawa | 7d88bb4 | 2011-07-30 18:02:29 +0900 | [diff] [blame] | 914 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 915 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 916 | while (size--) { |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 917 | rc = do_insn_fetch_byte(ctxt, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 918 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 919 | return rc; |
| 920 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 921 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 924 | /* Fetch next part of the instruction being emulated. */ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 925 | #define insn_fetch(_type, _ctxt) \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 926 | ({ unsigned long _x; \ |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 927 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 928 | if (rc != X86EMUL_CONTINUE) \ |
| 929 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 930 | (_type)_x; \ |
| 931 | }) |
| 932 | |
Takuya Yoshikawa | 807941b | 2011-07-30 18:00:17 +0900 | [diff] [blame] | 933 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
| 934 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 935 | if (rc != X86EMUL_CONTINUE) \ |
| 936 | goto done; \ |
Takuya Yoshikawa | 67cbc90 | 2011-05-15 00:54:58 +0900 | [diff] [blame] | 937 | }) |
| 938 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 939 | /* |
| 940 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 941 | * pointer into the block that addresses the relevant register. |
| 942 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 943 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 944 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 945 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 946 | { |
| 947 | void *p; |
| 948 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 949 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 950 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
| 951 | else |
| 952 | p = reg_rmw(ctxt, modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 953 | return p; |
| 954 | } |
| 955 | |
| 956 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 957 | struct segmented_address addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 958 | u16 *size, unsigned long *address, int op_bytes) |
| 959 | { |
| 960 | int rc; |
| 961 | |
| 962 | if (op_bytes == 2) |
| 963 | op_bytes = 3; |
| 964 | *address = 0; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 965 | rc = segmented_read_std(ctxt, addr, size, 2); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 966 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 967 | return rc; |
Avi Kivity | 30b31ab | 2010-11-17 15:28:22 +0200 | [diff] [blame] | 968 | addr.ea += 2; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 969 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | return rc; |
| 971 | } |
| 972 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 973 | static u8 test_cc(unsigned int condition, unsigned long flags) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 974 | { |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 975 | u8 rc; |
| 976 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 977 | |
Avi Kivity | 9ae9feb | 2013-01-19 19:51:52 +0200 | [diff] [blame] | 978 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
| 979 | asm("pushq %[flags]; popf; call *%[fastop]" |
| 980 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); |
| 981 | return rc; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 982 | } |
| 983 | |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 984 | static void fetch_register_operand(struct operand *op) |
| 985 | { |
| 986 | switch (op->bytes) { |
| 987 | case 1: |
| 988 | op->val = *(u8 *)op->addr.reg; |
| 989 | break; |
| 990 | case 2: |
| 991 | op->val = *(u16 *)op->addr.reg; |
| 992 | break; |
| 993 | case 4: |
| 994 | op->val = *(u32 *)op->addr.reg; |
| 995 | break; |
| 996 | case 8: |
| 997 | op->val = *(u64 *)op->addr.reg; |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1002 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
| 1003 | { |
| 1004 | ctxt->ops->get_fpu(ctxt); |
| 1005 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1006 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
| 1007 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; |
| 1008 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; |
| 1009 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; |
| 1010 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; |
| 1011 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; |
| 1012 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; |
| 1013 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1014 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1015 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
| 1016 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; |
| 1017 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; |
| 1018 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; |
| 1019 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; |
| 1020 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; |
| 1021 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; |
| 1022 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1023 | #endif |
| 1024 | default: BUG(); |
| 1025 | } |
| 1026 | ctxt->ops->put_fpu(ctxt); |
| 1027 | } |
| 1028 | |
| 1029 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, |
| 1030 | int reg) |
| 1031 | { |
| 1032 | ctxt->ops->get_fpu(ctxt); |
| 1033 | switch (reg) { |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1034 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
| 1035 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; |
| 1036 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; |
| 1037 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; |
| 1038 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; |
| 1039 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; |
| 1040 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; |
| 1041 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1042 | #ifdef CONFIG_X86_64 |
Mathias Krause | 89a87c6 | 2012-08-30 01:30:14 +0200 | [diff] [blame] | 1043 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
| 1044 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; |
| 1045 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; |
| 1046 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; |
| 1047 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; |
| 1048 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; |
| 1049 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; |
| 1050 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1051 | #endif |
| 1052 | default: BUG(); |
| 1053 | } |
| 1054 | ctxt->ops->put_fpu(ctxt); |
| 1055 | } |
| 1056 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1057 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1058 | { |
| 1059 | ctxt->ops->get_fpu(ctxt); |
| 1060 | switch (reg) { |
| 1061 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; |
| 1062 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; |
| 1063 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; |
| 1064 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; |
| 1065 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; |
| 1066 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; |
| 1067 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; |
| 1068 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; |
| 1069 | default: BUG(); |
| 1070 | } |
| 1071 | ctxt->ops->put_fpu(ctxt); |
| 1072 | } |
| 1073 | |
| 1074 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
| 1075 | { |
| 1076 | ctxt->ops->get_fpu(ctxt); |
| 1077 | switch (reg) { |
| 1078 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; |
| 1079 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; |
| 1080 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; |
| 1081 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; |
| 1082 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; |
| 1083 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; |
| 1084 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; |
| 1085 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; |
| 1086 | default: BUG(); |
| 1087 | } |
| 1088 | ctxt->ops->put_fpu(ctxt); |
| 1089 | } |
| 1090 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 1091 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
| 1092 | { |
| 1093 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1094 | return emulate_nm(ctxt); |
| 1095 | |
| 1096 | ctxt->ops->get_fpu(ctxt); |
| 1097 | asm volatile("fninit"); |
| 1098 | ctxt->ops->put_fpu(ctxt); |
| 1099 | return X86EMUL_CONTINUE; |
| 1100 | } |
| 1101 | |
| 1102 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) |
| 1103 | { |
| 1104 | u16 fcw; |
| 1105 | |
| 1106 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1107 | return emulate_nm(ctxt); |
| 1108 | |
| 1109 | ctxt->ops->get_fpu(ctxt); |
| 1110 | asm volatile("fnstcw %0": "+m"(fcw)); |
| 1111 | ctxt->ops->put_fpu(ctxt); |
| 1112 | |
| 1113 | /* force 2 byte destination */ |
| 1114 | ctxt->dst.bytes = 2; |
| 1115 | ctxt->dst.val = fcw; |
| 1116 | |
| 1117 | return X86EMUL_CONTINUE; |
| 1118 | } |
| 1119 | |
| 1120 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) |
| 1121 | { |
| 1122 | u16 fsw; |
| 1123 | |
| 1124 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) |
| 1125 | return emulate_nm(ctxt); |
| 1126 | |
| 1127 | ctxt->ops->get_fpu(ctxt); |
| 1128 | asm volatile("fnstsw %0": "+m"(fsw)); |
| 1129 | ctxt->ops->put_fpu(ctxt); |
| 1130 | |
| 1131 | /* force 2 byte destination */ |
| 1132 | ctxt->dst.bytes = 2; |
| 1133 | ctxt->dst.val = fsw; |
| 1134 | |
| 1135 | return X86EMUL_CONTINUE; |
| 1136 | } |
| 1137 | |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1138 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1139 | struct operand *op) |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1140 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1141 | unsigned reg = ctxt->modrm_reg; |
| 1142 | int highbyte_regs = ctxt->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1143 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1144 | if (!(ctxt->d & ModRM)) |
| 1145 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1146 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1147 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1148 | op->type = OP_XMM; |
| 1149 | op->bytes = 16; |
| 1150 | op->addr.xmm = reg; |
| 1151 | read_sse_reg(ctxt, &op->vec_val, reg); |
| 1152 | return; |
| 1153 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1154 | if (ctxt->d & Mmx) { |
| 1155 | reg &= 7; |
| 1156 | op->type = OP_MM; |
| 1157 | op->bytes = 8; |
| 1158 | op->addr.mm = reg; |
| 1159 | return; |
| 1160 | } |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1161 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1162 | op->type = OP_REG; |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 1163 | if (ctxt->d & ByteOp) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1164 | op->addr.reg = decode_register(ctxt, reg, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1165 | op->bytes = 1; |
| 1166 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1167 | op->addr.reg = decode_register(ctxt, reg, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1168 | op->bytes = ctxt->op_bytes; |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1169 | } |
Avi Kivity | 91ff3cb | 2010-08-01 12:53:09 +0300 | [diff] [blame] | 1170 | fetch_register_operand(op); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1171 | op->orig_val = op->val; |
| 1172 | } |
| 1173 | |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1174 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
| 1175 | { |
| 1176 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) |
| 1177 | ctxt->modrm_seg = VCPU_SREG_SS; |
| 1178 | } |
| 1179 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1180 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1181 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1182 | { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1183 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1184 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1185 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1186 | ulong modrm_ea = 0; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1187 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1188 | if (ctxt->rex_prefix) { |
| 1189 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ |
| 1190 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ |
| 1191 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1194 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
| 1195 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; |
| 1196 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); |
| 1197 | ctxt->modrm_seg = VCPU_SREG_DS; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1198 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1199 | if (ctxt->modrm_mod == 3) { |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1200 | op->type = OP_REG; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1201 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1202 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1203 | if (ctxt->d & Sse) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1204 | op->type = OP_XMM; |
| 1205 | op->bytes = 16; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1206 | op->addr.xmm = ctxt->modrm_rm; |
| 1207 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1208 | return rc; |
| 1209 | } |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1210 | if (ctxt->d & Mmx) { |
| 1211 | op->type = OP_MM; |
| 1212 | op->bytes = 8; |
| 1213 | op->addr.xmm = ctxt->modrm_rm & 7; |
| 1214 | return rc; |
| 1215 | } |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1216 | fetch_register_operand(op); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1217 | return rc; |
| 1218 | } |
| 1219 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1220 | op->type = OP_MEM; |
| 1221 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1222 | if (ctxt->ad_bytes == 2) { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1223 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 1224 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1225 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); |
| 1226 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1227 | |
| 1228 | /* 16-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1229 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1230 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1231 | if (ctxt->modrm_rm == 6) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1232 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1233 | break; |
| 1234 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1235 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1236 | break; |
| 1237 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1238 | modrm_ea += insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1239 | break; |
| 1240 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1241 | switch (ctxt->modrm_rm) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1242 | case 0: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1243 | modrm_ea += bx + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1244 | break; |
| 1245 | case 1: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1246 | modrm_ea += bx + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1247 | break; |
| 1248 | case 2: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1249 | modrm_ea += bp + si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1250 | break; |
| 1251 | case 3: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1252 | modrm_ea += bp + di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1253 | break; |
| 1254 | case 4: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1255 | modrm_ea += si; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1256 | break; |
| 1257 | case 5: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1258 | modrm_ea += di; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1259 | break; |
| 1260 | case 6: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1261 | if (ctxt->modrm_mod != 0) |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1262 | modrm_ea += bp; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1263 | break; |
| 1264 | case 7: |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1265 | modrm_ea += bx; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1266 | break; |
| 1267 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1268 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
| 1269 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) |
| 1270 | ctxt->modrm_seg = VCPU_SREG_SS; |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1271 | modrm_ea = (u16)modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1272 | } else { |
| 1273 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1274 | if ((ctxt->modrm_rm & 7) == 4) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1275 | sib = insn_fetch(u8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1276 | index_reg |= (sib >> 3) & 7; |
| 1277 | base_reg |= sib & 7; |
| 1278 | scale = sib >> 6; |
| 1279 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1280 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1281 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1282 | else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1283 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1284 | adjust_modrm_seg(ctxt, base_reg); |
| 1285 | } |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 1286 | if (index_reg != 4) |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1287 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1288 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 1289 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1290 | ctxt->rip_relative = 1; |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1291 | } else { |
| 1292 | base_reg = ctxt->modrm_rm; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1293 | modrm_ea += reg_read(ctxt, base_reg); |
Avi Kivity | a6e3407 | 2012-06-10 17:15:39 +0300 | [diff] [blame] | 1294 | adjust_modrm_seg(ctxt, base_reg); |
| 1295 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1296 | switch (ctxt->modrm_mod) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1297 | case 0: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1298 | if (ctxt->modrm_rm == 5) |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1299 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1300 | break; |
| 1301 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1302 | modrm_ea += insn_fetch(s8, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1303 | break; |
| 1304 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1305 | modrm_ea += insn_fetch(s32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1306 | break; |
| 1307 | } |
| 1308 | } |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1309 | op->addr.mem.ea = modrm_ea; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1310 | done: |
| 1311 | return rc; |
| 1312 | } |
| 1313 | |
| 1314 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1315 | struct operand *op) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1316 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1317 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1318 | |
Avi Kivity | 2dbd0dd | 2010-08-01 15:40:19 +0300 | [diff] [blame] | 1319 | op->type = OP_MEM; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1320 | switch (ctxt->ad_bytes) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1321 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1322 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1323 | break; |
| 1324 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1325 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1326 | break; |
| 1327 | case 8: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 1328 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1329 | break; |
| 1330 | } |
| 1331 | done: |
| 1332 | return rc; |
| 1333 | } |
| 1334 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1335 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1336 | { |
Sheng Yang | 7129eec | 2010-09-28 16:33:32 +0800 | [diff] [blame] | 1337 | long sv = 0, mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1338 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1339 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
| 1340 | mask = ~(ctxt->dst.bytes * 8 - 1); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1341 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1342 | if (ctxt->src.bytes == 2) |
| 1343 | sv = (s16)ctxt->src.val & (s16)mask; |
| 1344 | else if (ctxt->src.bytes == 4) |
| 1345 | sv = (s32)ctxt->src.val & (s32)mask; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1346 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1347 | ctxt->dst.addr.mem.ea += (sv >> 3); |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1348 | } |
Wei Yongjun | ba7ff2b | 2010-08-09 11:39:14 +0800 | [diff] [blame] | 1349 | |
| 1350 | /* only subword offset */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1351 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
Wei Yongjun | 35c843c | 2010-08-09 11:34:56 +0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1354 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1355 | unsigned long addr, void *dest, unsigned size) |
| 1356 | { |
| 1357 | int rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1358 | struct read_cache *mc = &ctxt->mem_read; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1359 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1360 | if (mc->pos < mc->end) |
| 1361 | goto read_cached; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1362 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1363 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1364 | |
Xiao Guangrong | f23b070 | 2012-07-26 13:12:22 +0800 | [diff] [blame] | 1365 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, |
| 1366 | &ctxt->exception); |
| 1367 | if (rc != X86EMUL_CONTINUE) |
| 1368 | return rc; |
| 1369 | |
| 1370 | mc->end += size; |
| 1371 | |
| 1372 | read_cached: |
| 1373 | memcpy(dest, mc->data + mc->pos, size); |
| 1374 | mc->pos += size; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1375 | return X86EMUL_CONTINUE; |
| 1376 | } |
| 1377 | |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1378 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
| 1379 | struct segmented_address addr, |
| 1380 | void *data, |
| 1381 | unsigned size) |
| 1382 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1383 | int rc; |
| 1384 | ulong linear; |
| 1385 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1386 | rc = linearize(ctxt, addr, size, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1387 | if (rc != X86EMUL_CONTINUE) |
| 1388 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1389 | return read_emulated(ctxt, linear, data, size); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | static int segmented_write(struct x86_emulate_ctxt *ctxt, |
| 1393 | struct segmented_address addr, |
| 1394 | const void *data, |
| 1395 | unsigned size) |
| 1396 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1397 | int rc; |
| 1398 | ulong linear; |
| 1399 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1400 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1401 | if (rc != X86EMUL_CONTINUE) |
| 1402 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1403 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
| 1404 | &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, |
| 1408 | struct segmented_address addr, |
| 1409 | const void *orig_data, const void *data, |
| 1410 | unsigned size) |
| 1411 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1412 | int rc; |
| 1413 | ulong linear; |
| 1414 | |
Avi Kivity | 83b8795 | 2011-04-03 11:31:19 +0300 | [diff] [blame] | 1415 | rc = linearize(ctxt, addr, size, true, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 1416 | if (rc != X86EMUL_CONTINUE) |
| 1417 | return rc; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1418 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
| 1419 | size, &ctxt->exception); |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1422 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1423 | unsigned int size, unsigned short port, |
| 1424 | void *dest) |
| 1425 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1426 | struct read_cache *rc = &ctxt->io_read; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1427 | |
| 1428 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1429 | unsigned int in_page, n; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1430 | unsigned int count = ctxt->rep_prefix ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1431 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1432 | in_page = (ctxt->eflags & EFLG_DF) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1433 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
| 1434 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1435 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1436 | count); |
| 1437 | if (n == 0) |
| 1438 | n = 1; |
| 1439 | rc->pos = rc->end = 0; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1440 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1441 | return 0; |
| 1442 | rc->end = n * size; |
| 1443 | } |
| 1444 | |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1445 | if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) { |
| 1446 | ctxt->dst.data = rc->data + rc->pos; |
| 1447 | ctxt->dst.type = OP_MEM_STR; |
| 1448 | ctxt->dst.count = (rc->end - rc->pos) / size; |
| 1449 | rc->pos = rc->end; |
| 1450 | } else { |
| 1451 | memcpy(dest, rc->data + rc->pos, size); |
| 1452 | rc->pos += size; |
| 1453 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1454 | return 1; |
| 1455 | } |
| 1456 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 1457 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1458 | u16 index, struct desc_struct *desc) |
| 1459 | { |
| 1460 | struct desc_ptr dt; |
| 1461 | ulong addr; |
| 1462 | |
| 1463 | ctxt->ops->get_idt(ctxt, &dt); |
| 1464 | |
| 1465 | if (dt.size < index * 8 + 7) |
| 1466 | return emulate_gp(ctxt, index << 3 | 0x2); |
| 1467 | |
| 1468 | addr = dt.address + index * 8; |
| 1469 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1470 | &ctxt->exception); |
| 1471 | } |
| 1472 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1473 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1474 | u16 selector, struct desc_ptr *dt) |
| 1475 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1476 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1477 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1478 | if (selector & 1 << 2) { |
| 1479 | struct desc_struct desc; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1480 | u16 sel; |
| 1481 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1482 | memset (dt, 0, sizeof *dt); |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 1483 | if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR)) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1484 | return; |
| 1485 | |
| 1486 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1487 | dt->address = get_desc_base(&desc); |
| 1488 | } else |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1489 | ops->get_gdt(ctxt, dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* allowed just for 8 bytes segments */ |
| 1493 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1494 | u16 selector, struct desc_struct *desc, |
| 1495 | ulong *desc_addr_p) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1496 | { |
| 1497 | struct desc_ptr dt; |
| 1498 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1499 | ulong addr; |
| 1500 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1501 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1502 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1503 | if (dt.size < index * 8 + 7) |
| 1504 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1505 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1506 | *desc_addr_p = addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1507 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
| 1508 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /* allowed just for 8 bytes segments */ |
| 1512 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1513 | u16 selector, struct desc_struct *desc) |
| 1514 | { |
| 1515 | struct desc_ptr dt; |
| 1516 | u16 index = selector >> 3; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1517 | ulong addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1518 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1519 | get_descriptor_table_ptr(ctxt, selector, &dt); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1520 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1521 | if (dt.size < index * 8 + 7) |
| 1522 | return emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1523 | |
| 1524 | addr = dt.address + index * 8; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1525 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
| 1526 | &ctxt->exception); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 1529 | /* Does not support long mode */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1530 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1531 | u16 selector, int seg) |
| 1532 | { |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1533 | struct desc_struct seg_desc, old_desc; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1534 | u8 dpl, rpl, cpl; |
| 1535 | unsigned err_vec = GP_VECTOR; |
| 1536 | u32 err_code = 0; |
| 1537 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1538 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1539 | int ret; |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1540 | u16 dummy; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1541 | |
| 1542 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1543 | |
| 1544 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1545 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1546 | /* set real mode segment descriptor */ |
Avi Kivity | 03ebebe | 2012-08-21 17:07:04 +0300 | [diff] [blame] | 1547 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1548 | set_desc_base(&seg_desc, selector << 4); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1549 | goto load; |
| 1550 | } |
| 1551 | |
Avi Kivity | 79d5b4c | 2012-06-07 17:03:42 +0300 | [diff] [blame] | 1552 | rpl = selector & 3; |
| 1553 | cpl = ctxt->ops->cpl(ctxt); |
| 1554 | |
| 1555 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ |
| 1556 | if ((seg == VCPU_SREG_CS |
| 1557 | || (seg == VCPU_SREG_SS |
| 1558 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) |
| 1559 | || seg == VCPU_SREG_TR) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1560 | && null_selector) |
| 1561 | goto exception; |
| 1562 | |
| 1563 | /* TR should be in GDT only */ |
| 1564 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1565 | goto exception; |
| 1566 | |
| 1567 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1568 | goto load; |
| 1569 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 1570 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1571 | if (ret != X86EMUL_CONTINUE) |
| 1572 | return ret; |
| 1573 | |
| 1574 | err_code = selector & 0xfffc; |
| 1575 | err_vec = GP_VECTOR; |
| 1576 | |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 1577 | /* can't load system descriptor into segment selector */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1578 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1579 | goto exception; |
| 1580 | |
| 1581 | if (!seg_desc.p) { |
| 1582 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1583 | goto exception; |
| 1584 | } |
| 1585 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1586 | dpl = seg_desc.dpl; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1587 | |
| 1588 | switch (seg) { |
| 1589 | case VCPU_SREG_SS: |
| 1590 | /* |
| 1591 | * segment is not a writable data segment or segment |
| 1592 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1593 | */ |
| 1594 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1595 | goto exception; |
| 1596 | break; |
| 1597 | case VCPU_SREG_CS: |
| 1598 | if (!(seg_desc.type & 8)) |
| 1599 | goto exception; |
| 1600 | |
| 1601 | if (seg_desc.type & 4) { |
| 1602 | /* conforming */ |
| 1603 | if (dpl > cpl) |
| 1604 | goto exception; |
| 1605 | } else { |
| 1606 | /* nonconforming */ |
| 1607 | if (rpl > cpl || dpl != cpl) |
| 1608 | goto exception; |
| 1609 | } |
| 1610 | /* CS(RPL) <- CPL */ |
| 1611 | selector = (selector & 0xfffc) | cpl; |
| 1612 | break; |
| 1613 | case VCPU_SREG_TR: |
| 1614 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1615 | goto exception; |
Avi Kivity | 869be99 | 2012-06-13 16:30:53 +0300 | [diff] [blame] | 1616 | old_desc = seg_desc; |
| 1617 | seg_desc.type |= 2; /* busy */ |
| 1618 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, |
| 1619 | sizeof(seg_desc), &ctxt->exception); |
| 1620 | if (ret != X86EMUL_CONTINUE) |
| 1621 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1622 | break; |
| 1623 | case VCPU_SREG_LDTR: |
| 1624 | if (seg_desc.s || seg_desc.type != 2) |
| 1625 | goto exception; |
| 1626 | break; |
| 1627 | default: /* DS, ES, FS, or GS */ |
| 1628 | /* |
| 1629 | * segment is not a data or readable code segment or |
| 1630 | * ((segment is a data or nonconforming code segment) |
| 1631 | * and (both RPL and CPL > DPL)) |
| 1632 | */ |
| 1633 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1634 | (((seg_desc.type & 0xc) != 0xc) && |
| 1635 | (rpl > dpl && cpl > dpl))) |
| 1636 | goto exception; |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | if (seg_desc.s) { |
| 1641 | /* mark segment as accessed */ |
| 1642 | seg_desc.type |= 1; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1643 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1644 | if (ret != X86EMUL_CONTINUE) |
| 1645 | return ret; |
| 1646 | } |
| 1647 | load: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1648 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1649 | return X86EMUL_CONTINUE; |
| 1650 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1651 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1652 | return X86EMUL_PROPAGATE_FAULT; |
| 1653 | } |
| 1654 | |
Wei Yongjun | 31be40b | 2010-08-17 09:17:30 +0800 | [diff] [blame] | 1655 | static void write_register_operand(struct operand *op) |
| 1656 | { |
| 1657 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ |
| 1658 | switch (op->bytes) { |
| 1659 | case 1: |
| 1660 | *(u8 *)op->addr.reg = (u8)op->val; |
| 1661 | break; |
| 1662 | case 2: |
| 1663 | *(u16 *)op->addr.reg = (u16)op->val; |
| 1664 | break; |
| 1665 | case 4: |
| 1666 | *op->addr.reg = (u32)op->val; |
| 1667 | break; /* 64b: zero-extend */ |
| 1668 | case 8: |
| 1669 | *op->addr.reg = op->val; |
| 1670 | break; |
| 1671 | } |
| 1672 | } |
| 1673 | |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 1674 | static int writeback(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1675 | { |
| 1676 | int rc; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1677 | |
Avi Kivity | b6744dc | 2013-01-04 16:18:50 +0200 | [diff] [blame] | 1678 | if (ctxt->d & NoWrite) |
| 1679 | return X86EMUL_CONTINUE; |
| 1680 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1681 | switch (ctxt->dst.type) { |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1682 | case OP_REG: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1683 | write_register_operand(&ctxt->dst); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1684 | break; |
| 1685 | case OP_MEM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1686 | if (ctxt->lock_prefix) |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1687 | rc = segmented_cmpxchg(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1688 | ctxt->dst.addr.mem, |
| 1689 | &ctxt->dst.orig_val, |
| 1690 | &ctxt->dst.val, |
| 1691 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1692 | else |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1693 | rc = segmented_write(ctxt, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1694 | ctxt->dst.addr.mem, |
| 1695 | &ctxt->dst.val, |
| 1696 | ctxt->dst.bytes); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1697 | if (rc != X86EMUL_CONTINUE) |
| 1698 | return rc; |
| 1699 | break; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 1700 | case OP_MEM_STR: |
| 1701 | rc = segmented_write(ctxt, |
| 1702 | ctxt->dst.addr.mem, |
| 1703 | ctxt->dst.data, |
| 1704 | ctxt->dst.bytes * ctxt->dst.count); |
| 1705 | if (rc != X86EMUL_CONTINUE) |
| 1706 | return rc; |
| 1707 | break; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1708 | case OP_XMM: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1709 | write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm); |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 1710 | break; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 1711 | case OP_MM: |
| 1712 | write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm); |
| 1713 | break; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1714 | case OP_NONE: |
| 1715 | /* no writeback */ |
| 1716 | break; |
| 1717 | default: |
| 1718 | break; |
| 1719 | } |
| 1720 | return X86EMUL_CONTINUE; |
| 1721 | } |
| 1722 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1723 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1724 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1725 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1726 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1727 | rsp_increment(ctxt, -bytes); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1728 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1729 | addr.seg = VCPU_SREG_SS; |
| 1730 | |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1731 | return segmented_write(ctxt, addr, data, bytes); |
| 1732 | } |
| 1733 | |
| 1734 | static int em_push(struct x86_emulate_ctxt *ctxt) |
| 1735 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 1736 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1737 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 51ddff5 | 2012-06-12 20:19:40 +0300 | [diff] [blame] | 1738 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1741 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1742 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1743 | { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1744 | int rc; |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1745 | struct segmented_address addr; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1746 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1747 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
Avi Kivity | 90de84f | 2010-11-17 15:28:21 +0200 | [diff] [blame] | 1748 | addr.seg = VCPU_SREG_SS; |
Avi Kivity | 3ca3ac4 | 2011-03-31 16:52:26 +0200 | [diff] [blame] | 1749 | rc = segmented_read(ctxt, addr, dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1750 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1751 | return rc; |
| 1752 | |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1753 | rsp_increment(ctxt, len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1754 | return rc; |
| 1755 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1756 | |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1757 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
| 1758 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1759 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 1760 | } |
| 1761 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1762 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1763 | void *dest, int len) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1764 | { |
| 1765 | int rc; |
| 1766 | unsigned long val, change_mask; |
| 1767 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1768 | int cpl = ctxt->ops->cpl(ctxt); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1769 | |
Takuya Yoshikawa | 3b9be3b | 2011-05-02 02:27:55 +0900 | [diff] [blame] | 1770 | rc = emulate_pop(ctxt, &val, len); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1771 | if (rc != X86EMUL_CONTINUE) |
| 1772 | return rc; |
| 1773 | |
| 1774 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1775 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1776 | |
| 1777 | switch(ctxt->mode) { |
| 1778 | case X86EMUL_MODE_PROT64: |
| 1779 | case X86EMUL_MODE_PROT32: |
| 1780 | case X86EMUL_MODE_PROT16: |
| 1781 | if (cpl == 0) |
| 1782 | change_mask |= EFLG_IOPL; |
| 1783 | if (cpl <= iopl) |
| 1784 | change_mask |= EFLG_IF; |
| 1785 | break; |
| 1786 | case X86EMUL_MODE_VM86: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 1787 | if (iopl < 3) |
| 1788 | return emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1789 | change_mask |= EFLG_IF; |
| 1790 | break; |
| 1791 | default: /* real mode */ |
| 1792 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | *(unsigned long *)dest = |
| 1797 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1798 | |
| 1799 | return rc; |
| 1800 | } |
| 1801 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1802 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
| 1803 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1804 | ctxt->dst.type = OP_REG; |
| 1805 | ctxt->dst.addr.reg = &ctxt->eflags; |
| 1806 | ctxt->dst.bytes = ctxt->op_bytes; |
| 1807 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1808 | } |
| 1809 | |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1810 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
| 1811 | { |
| 1812 | int rc; |
| 1813 | unsigned frame_size = ctxt->src.val; |
| 1814 | unsigned nesting_level = ctxt->src2.val & 31; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1815 | ulong rbp; |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1816 | |
| 1817 | if (nesting_level) |
| 1818 | return X86EMUL_UNHANDLEABLE; |
| 1819 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1820 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
| 1821 | rc = push(ctxt, &rbp, stack_size(ctxt)); |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1822 | if (rc != X86EMUL_CONTINUE) |
| 1823 | return rc; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1824 | 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] | 1825 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1826 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
| 1827 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 1828 | stack_mask(ctxt)); |
| 1829 | return X86EMUL_CONTINUE; |
| 1830 | } |
| 1831 | |
Avi Kivity | f47cfa3 | 2012-06-07 17:49:24 +0300 | [diff] [blame] | 1832 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
| 1833 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1834 | 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] | 1835 | stack_mask(ctxt)); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1836 | 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] | 1837 | } |
| 1838 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1839 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1840 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1841 | int seg = ctxt->src2.val; |
| 1842 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1843 | ctxt->src.val = get_segment_selector(ctxt, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1844 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1845 | return em_push(ctxt); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1846 | } |
| 1847 | |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1848 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1849 | { |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 1850 | int seg = ctxt->src2.val; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1851 | unsigned long selector; |
| 1852 | int rc; |
| 1853 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1854 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1855 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1856 | return rc; |
| 1857 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1858 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1859 | return rc; |
| 1860 | } |
| 1861 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1862 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1863 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1864 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1865 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1866 | int reg = VCPU_REGS_RAX; |
| 1867 | |
| 1868 | while (reg <= VCPU_REGS_RDI) { |
| 1869 | (reg == VCPU_REGS_RSP) ? |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1870 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1871 | |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1872 | rc = em_push(ctxt); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1873 | if (rc != X86EMUL_CONTINUE) |
| 1874 | return rc; |
| 1875 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1876 | ++reg; |
| 1877 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1878 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1879 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1882 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
| 1883 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1884 | ctxt->src.val = (unsigned long)ctxt->eflags; |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 1885 | return em_push(ctxt); |
| 1886 | } |
| 1887 | |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 1888 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1889 | { |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1890 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1891 | int reg = VCPU_REGS_RDI; |
| 1892 | |
| 1893 | while (reg >= VCPU_REGS_RAX) { |
| 1894 | if (reg == VCPU_REGS_RSP) { |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 1895 | rsp_increment(ctxt, ctxt->op_bytes); |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1896 | --reg; |
| 1897 | } |
| 1898 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1899 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1900 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1901 | break; |
| 1902 | --reg; |
| 1903 | } |
| 1904 | return rc; |
| 1905 | } |
| 1906 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1907 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1908 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 1909 | const struct x86_emulate_ops *ops = ctxt->ops; |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1910 | int rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1911 | struct desc_ptr dt; |
| 1912 | gva_t cs_addr; |
| 1913 | gva_t eip_addr; |
| 1914 | u16 cs, eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1915 | |
| 1916 | /* TODO: Add limit checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1917 | ctxt->src.val = ctxt->eflags; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1918 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1919 | if (rc != X86EMUL_CONTINUE) |
| 1920 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1921 | |
| 1922 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); |
| 1923 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1924 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1925 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1926 | if (rc != X86EMUL_CONTINUE) |
| 1927 | return rc; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1928 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1929 | ctxt->src.val = ctxt->_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 1930 | rc = em_push(ctxt); |
Avi Kivity | 5c56e1c | 2010-08-17 11:17:51 +0300 | [diff] [blame] | 1931 | if (rc != X86EMUL_CONTINUE) |
| 1932 | return rc; |
| 1933 | |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1934 | ops->get_idt(ctxt, &dt); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1935 | |
| 1936 | eip_addr = dt.address + (irq << 2); |
| 1937 | cs_addr = dt.address + (irq << 2) + 2; |
| 1938 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1939 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1940 | if (rc != X86EMUL_CONTINUE) |
| 1941 | return rc; |
| 1942 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 1943 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1944 | if (rc != X86EMUL_CONTINUE) |
| 1945 | return rc; |
| 1946 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1947 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1948 | if (rc != X86EMUL_CONTINUE) |
| 1949 | return rc; |
| 1950 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1951 | ctxt->_eip = eip; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1952 | |
| 1953 | return rc; |
| 1954 | } |
| 1955 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1956 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
| 1957 | { |
| 1958 | int rc; |
| 1959 | |
| 1960 | invalidate_registers(ctxt); |
| 1961 | rc = __emulate_int_real(ctxt, irq); |
| 1962 | if (rc == X86EMUL_CONTINUE) |
| 1963 | writeback_registers(ctxt); |
| 1964 | return rc; |
| 1965 | } |
| 1966 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1967 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1968 | { |
| 1969 | switch(ctxt->mode) { |
| 1970 | case X86EMUL_MODE_REAL: |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 1971 | return __emulate_int_real(ctxt, irq); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 1972 | case X86EMUL_MODE_VM86: |
| 1973 | case X86EMUL_MODE_PROT16: |
| 1974 | case X86EMUL_MODE_PROT32: |
| 1975 | case X86EMUL_MODE_PROT64: |
| 1976 | default: |
| 1977 | /* Protected mode interrupts unimplemented yet */ |
| 1978 | return X86EMUL_UNHANDLEABLE; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 1982 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1983 | { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1984 | int rc = X86EMUL_CONTINUE; |
| 1985 | unsigned long temp_eip = 0; |
| 1986 | unsigned long temp_eflags = 0; |
| 1987 | unsigned long cs = 0; |
| 1988 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1989 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1990 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1991 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1992 | |
| 1993 | /* TODO: Add stack limit check */ |
| 1994 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 1995 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1996 | |
| 1997 | if (rc != X86EMUL_CONTINUE) |
| 1998 | return rc; |
| 1999 | |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2000 | if (temp_eip & ~0xffff) |
| 2001 | return emulate_gp(ctxt, 0); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2002 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2003 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2004 | |
| 2005 | if (rc != X86EMUL_CONTINUE) |
| 2006 | return rc; |
| 2007 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2008 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2009 | |
| 2010 | if (rc != X86EMUL_CONTINUE) |
| 2011 | return rc; |
| 2012 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2013 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2014 | |
| 2015 | if (rc != X86EMUL_CONTINUE) |
| 2016 | return rc; |
| 2017 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2018 | ctxt->_eip = temp_eip; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2019 | |
| 2020 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2021 | if (ctxt->op_bytes == 4) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2022 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2023 | else if (ctxt->op_bytes == 2) { |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2024 | ctxt->eflags &= ~0xffff; |
| 2025 | ctxt->eflags |= temp_eflags; |
| 2026 | } |
| 2027 | |
| 2028 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 2029 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 2030 | |
| 2031 | return rc; |
| 2032 | } |
| 2033 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2034 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2035 | { |
| 2036 | switch(ctxt->mode) { |
| 2037 | case X86EMUL_MODE_REAL: |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2038 | return emulate_iret_real(ctxt); |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2039 | case X86EMUL_MODE_VM86: |
| 2040 | case X86EMUL_MODE_PROT16: |
| 2041 | case X86EMUL_MODE_PROT32: |
| 2042 | case X86EMUL_MODE_PROT64: |
| 2043 | default: |
| 2044 | /* iret from protected mode unimplemented yet */ |
| 2045 | return X86EMUL_UNHANDLEABLE; |
| 2046 | } |
| 2047 | } |
| 2048 | |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2049 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
| 2050 | { |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2051 | int rc; |
| 2052 | unsigned short sel; |
| 2053 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2054 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2055 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2056 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2057 | if (rc != X86EMUL_CONTINUE) |
| 2058 | return rc; |
| 2059 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2060 | ctxt->_eip = 0; |
| 2061 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2062 | return X86EMUL_CONTINUE; |
| 2063 | } |
| 2064 | |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 2065 | FASTOP1(not); |
| 2066 | FASTOP1(neg); |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 2067 | FASTOP1(inc); |
| 2068 | FASTOP1(dec); |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 2069 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 2070 | FASTOP2CL(rol); |
| 2071 | FASTOP2CL(ror); |
| 2072 | FASTOP2CL(rcl); |
| 2073 | FASTOP2CL(rcr); |
| 2074 | FASTOP2CL(shl); |
| 2075 | FASTOP2CL(shr); |
| 2076 | FASTOP2CL(sar); |
| 2077 | |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 2078 | FASTOP2W(bsf); |
| 2079 | FASTOP2W(bsr); |
| 2080 | FASTOP2W(bt); |
| 2081 | FASTOP2W(bts); |
| 2082 | FASTOP2W(btr); |
| 2083 | FASTOP2W(btc); |
| 2084 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 2085 | static int em_mul_ex(struct x86_emulate_ctxt *ctxt) |
| 2086 | { |
| 2087 | u8 ex = 0; |
| 2088 | |
| 2089 | emulate_1op_rax_rdx(ctxt, "mul", ex); |
| 2090 | return X86EMUL_CONTINUE; |
| 2091 | } |
| 2092 | |
| 2093 | static int em_imul_ex(struct x86_emulate_ctxt *ctxt) |
| 2094 | { |
| 2095 | u8 ex = 0; |
| 2096 | |
| 2097 | emulate_1op_rax_rdx(ctxt, "imul", ex); |
| 2098 | return X86EMUL_CONTINUE; |
| 2099 | } |
| 2100 | |
| 2101 | static int em_div_ex(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2102 | { |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 2103 | u8 de = 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2104 | |
Avi Kivity | 3329ece | 2011-09-13 10:45:39 +0300 | [diff] [blame] | 2105 | emulate_1op_rax_rdx(ctxt, "div", de); |
| 2106 | if (de) |
| 2107 | return emulate_de(ctxt); |
| 2108 | return X86EMUL_CONTINUE; |
| 2109 | } |
| 2110 | |
| 2111 | static int em_idiv_ex(struct x86_emulate_ctxt *ctxt) |
| 2112 | { |
| 2113 | u8 de = 0; |
| 2114 | |
| 2115 | emulate_1op_rax_rdx(ctxt, "idiv", de); |
Avi Kivity | 34d1f49 | 2010-08-26 11:59:01 +0300 | [diff] [blame] | 2116 | if (de) |
| 2117 | return emulate_de(ctxt); |
Mohammed Gamal | 8c5eee3 | 2010-08-08 21:11:38 +0300 | [diff] [blame] | 2118 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Takuya Yoshikawa | 5118768 | 2011-05-02 02:29:17 +0900 | [diff] [blame] | 2121 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2122 | { |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2123 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2124 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2125 | switch (ctxt->modrm_reg) { |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2126 | case 2: /* call near abs */ { |
| 2127 | long int old_eip; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2128 | old_eip = ctxt->_eip; |
| 2129 | ctxt->_eip = ctxt->src.val; |
| 2130 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2131 | rc = em_push(ctxt); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 2132 | break; |
| 2133 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2134 | case 4: /* jmp abs */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2135 | ctxt->_eip = ctxt->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2136 | break; |
Takuya Yoshikawa | d2f6276 | 2011-05-02 02:30:48 +0900 | [diff] [blame] | 2137 | case 5: /* jmp far */ |
| 2138 | rc = em_jmp_far(ctxt); |
| 2139 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2140 | case 6: /* push */ |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2141 | rc = em_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2142 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2143 | } |
Takuya Yoshikawa | 4179bb0 | 2011-04-13 00:29:09 +0900 | [diff] [blame] | 2144 | return rc; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2145 | } |
| 2146 | |
Takuya Yoshikawa | e0dac40 | 2011-12-06 18:07:27 +0900 | [diff] [blame] | 2147 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2148 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2149 | u64 old = ctxt->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2150 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2151 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
| 2152 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { |
| 2153 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); |
| 2154 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2155 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2156 | } else { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2157 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
| 2158 | (u32) reg_read(ctxt, VCPU_REGS_RBX); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2159 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2160 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2161 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2162 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2163 | } |
| 2164 | |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2165 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
| 2166 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2167 | ctxt->dst.type = OP_REG; |
| 2168 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 2169 | ctxt->dst.bytes = ctxt->op_bytes; |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 2170 | return em_pop(ctxt); |
| 2171 | } |
| 2172 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2173 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2174 | { |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2175 | int rc; |
| 2176 | unsigned long cs; |
| 2177 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2178 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2179 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2180 | return rc; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2181 | if (ctxt->op_bytes == 4) |
| 2182 | ctxt->_eip = (u32)ctxt->_eip; |
| 2183 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2184 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2185 | return rc; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2186 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2187 | return rc; |
| 2188 | } |
| 2189 | |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2190 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
| 2191 | { |
| 2192 | /* Save real source value, then compare EAX against destination. */ |
| 2193 | ctxt->src.orig_val = ctxt->src.val; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2194 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2195 | emulate_2op_SrcV(ctxt, "cmp"); |
| 2196 | |
| 2197 | if (ctxt->eflags & EFLG_ZF) { |
| 2198 | /* Success: write back to memory. */ |
| 2199 | ctxt->dst.val = ctxt->src.orig_val; |
| 2200 | } else { |
| 2201 | /* Failure: write the value we saw to EAX. */ |
| 2202 | ctxt->dst.type = OP_REG; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2203 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 2204 | } |
| 2205 | return X86EMUL_CONTINUE; |
| 2206 | } |
| 2207 | |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2208 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2209 | { |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 2210 | int seg = ctxt->src2.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2211 | unsigned short sel; |
| 2212 | int rc; |
| 2213 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2214 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2215 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2216 | rc = load_segment_descriptor(ctxt, sel, seg); |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2217 | if (rc != X86EMUL_CONTINUE) |
| 2218 | return rc; |
| 2219 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2220 | ctxt->dst.val = ctxt->src.val; |
Wei Yongjun | 09b5f4d | 2010-08-23 14:56:54 +0800 | [diff] [blame] | 2221 | return rc; |
| 2222 | } |
| 2223 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2224 | static void |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2225 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2226 | struct desc_struct *cs, struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2227 | { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2228 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2229 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2230 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2231 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2232 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 2233 | cs->s = 1; |
| 2234 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2235 | cs->p = 1; |
| 2236 | cs->d = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2237 | cs->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2238 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2239 | set_desc_base(ss, 0); /* flat segment */ |
| 2240 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2241 | ss->g = 1; /* 4kb granularity */ |
| 2242 | ss->s = 1; |
| 2243 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2244 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2245 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2246 | ss->p = 1; |
Gleb Natapov | 99245b5 | 2012-07-25 15:49:42 +0300 | [diff] [blame] | 2247 | ss->l = 0; |
| 2248 | ss->avl = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2251 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
| 2252 | { |
| 2253 | u32 eax, ebx, ecx, edx; |
| 2254 | |
| 2255 | eax = ecx = 0; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2256 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2257 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2258 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
| 2259 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; |
| 2260 | } |
| 2261 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2262 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
| 2263 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2264 | const struct x86_emulate_ops *ops = ctxt->ops; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2265 | u32 eax, ebx, ecx, edx; |
| 2266 | |
| 2267 | /* |
| 2268 | * syscall should always be enabled in longmode - so only become |
| 2269 | * vendor specific (cpuid) if other modes are active... |
| 2270 | */ |
| 2271 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2272 | return true; |
| 2273 | |
| 2274 | eax = 0x00000000; |
| 2275 | ecx = 0x00000000; |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2276 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
| 2277 | /* |
| 2278 | * Intel ("GenuineIntel") |
| 2279 | * remark: Intel CPUs only support "syscall" in 64bit |
| 2280 | * longmode. Also an 64bit guest with a |
| 2281 | * 32bit compat-app running will #UD !! While this |
| 2282 | * behaviour can be fixed (by emulating) into AMD |
| 2283 | * response - CPUs of AMD can't behave like Intel. |
| 2284 | */ |
| 2285 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && |
| 2286 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && |
| 2287 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) |
| 2288 | return false; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2289 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2290 | /* AMD ("AuthenticAMD") */ |
| 2291 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && |
| 2292 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && |
| 2293 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) |
| 2294 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2295 | |
Avi Kivity | 0017f93 | 2012-06-07 14:10:16 +0300 | [diff] [blame] | 2296 | /* AMD ("AMDisbetter!") */ |
| 2297 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && |
| 2298 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && |
| 2299 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) |
| 2300 | return true; |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2301 | |
| 2302 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ |
| 2303 | return false; |
| 2304 | } |
| 2305 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2306 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2307 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2308 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2309 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2310 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2311 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2312 | u64 efer = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2313 | |
| 2314 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2315 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2316 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2317 | return emulate_ud(ctxt); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2318 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2319 | if (!(em_syscall_is_enabled(ctxt))) |
| 2320 | return emulate_ud(ctxt); |
| 2321 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2322 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2323 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2324 | |
Stephan Bärwolf | c2226fc | 2012-01-12 16:43:04 +0100 | [diff] [blame] | 2325 | if (!(efer & EFER_SCE)) |
| 2326 | return emulate_ud(ctxt); |
| 2327 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2328 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2329 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2330 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2331 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2332 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2333 | if (efer & EFER_LMA) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2334 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2335 | cs.l = 1; |
| 2336 | } |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2337 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2338 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2339 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2340 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2341 | if (efer & EFER_LMA) { |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2342 | #ifdef CONFIG_X86_64 |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2343 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2344 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2345 | ops->get_msr(ctxt, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2346 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2347 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2348 | ctxt->_eip = msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2349 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2350 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2351 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2352 | #endif |
| 2353 | } else { |
| 2354 | /* legacy mode */ |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2355 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2356 | ctxt->_eip = (u32)msr_data; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2357 | |
| 2358 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2359 | } |
| 2360 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2361 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2364 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2365 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2366 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2367 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2368 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2369 | u16 cs_sel, ss_sel; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2370 | u64 efer = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2371 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2372 | ops->get_msr(ctxt, MSR_EFER, &efer); |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2373 | /* inject #GP if in real mode */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2374 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2375 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2376 | |
Avi Kivity | 1a18a69 | 2012-02-01 12:23:21 +0200 | [diff] [blame] | 2377 | /* |
| 2378 | * Not recognized on AMD in compat mode (but is recognized in legacy |
| 2379 | * mode). |
| 2380 | */ |
| 2381 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) |
| 2382 | && !vendor_intel(ctxt)) |
| 2383 | return emulate_ud(ctxt); |
| 2384 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2385 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2386 | * Therefore, we inject an #UD. |
| 2387 | */ |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2388 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 2389 | return emulate_ud(ctxt); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2390 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2391 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2392 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2393 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2394 | switch (ctxt->mode) { |
| 2395 | case X86EMUL_MODE_PROT32: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2396 | if ((msr_data & 0xfffc) == 0x0) |
| 2397 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2398 | break; |
| 2399 | case X86EMUL_MODE_PROT64: |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2400 | if (msr_data == 0x0) |
| 2401 | return emulate_gp(ctxt, 0); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2402 | break; |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 2403 | default: |
| 2404 | break; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2408 | cs_sel = (u16)msr_data; |
| 2409 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2410 | ss_sel = cs_sel + 8; |
| 2411 | ss_sel &= ~SELECTOR_RPL_MASK; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 2412 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2413 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2414 | cs.l = 1; |
| 2415 | } |
| 2416 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2417 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2418 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2419 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2420 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2421 | ctxt->_eip = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2422 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2423 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2424 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2425 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2426 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Takuya Yoshikawa | e01991e | 2011-05-29 21:55:10 +0900 | [diff] [blame] | 2429 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2430 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2431 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2432 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2433 | u64 msr_data; |
| 2434 | int usermode; |
Xiao Guangrong | 1249b96 | 2011-05-15 23:25:10 +0800 | [diff] [blame] | 2435 | u16 cs_sel = 0, ss_sel = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2436 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2437 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2438 | if (ctxt->mode == X86EMUL_MODE_REAL || |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2439 | ctxt->mode == X86EMUL_MODE_VM86) |
| 2440 | return emulate_gp(ctxt, 0); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2441 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2442 | setup_syscalls_segments(ctxt, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2443 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2444 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2445 | usermode = X86EMUL_MODE_PROT64; |
| 2446 | else |
| 2447 | usermode = X86EMUL_MODE_PROT32; |
| 2448 | |
| 2449 | cs.dpl = 3; |
| 2450 | ss.dpl = 3; |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2451 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2452 | switch (usermode) { |
| 2453 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2454 | cs_sel = (u16)(msr_data + 16); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2455 | if ((msr_data & 0xfffc) == 0x0) |
| 2456 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2457 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2458 | break; |
| 2459 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2460 | cs_sel = (u16)(msr_data + 32); |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2461 | if (msr_data == 0x0) |
| 2462 | return emulate_gp(ctxt, 0); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2463 | ss_sel = cs_sel + 8; |
| 2464 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2465 | cs.l = 1; |
| 2466 | break; |
| 2467 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2468 | cs_sel |= SELECTOR_RPL_MASK; |
| 2469 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2470 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2471 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
| 2472 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2473 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2474 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
| 2475 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2476 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2477 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2478 | } |
| 2479 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2480 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2481 | { |
| 2482 | int iopl; |
| 2483 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2484 | return false; |
| 2485 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2486 | return true; |
| 2487 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2488 | return ctxt->ops->cpl(ctxt) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2492 | u16 port, u16 len) |
| 2493 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2494 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2495 | struct desc_struct tr_seg; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2496 | u32 base3; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2497 | int r; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2498 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2499 | unsigned mask = (1 << len) - 1; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2500 | unsigned long base; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2501 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2502 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2503 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2504 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2505 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2506 | return false; |
Gleb Natapov | 5601d05 | 2011-03-07 14:55:06 +0200 | [diff] [blame] | 2507 | base = get_desc_base(&tr_seg); |
| 2508 | #ifdef CONFIG_X86_64 |
| 2509 | base |= ((u64)base3) << 32; |
| 2510 | #endif |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2511 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2512 | if (r != X86EMUL_CONTINUE) |
| 2513 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2514 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2515 | return false; |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2516 | 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] | 2517 | if (r != X86EMUL_CONTINUE) |
| 2518 | return false; |
| 2519 | if ((perm >> bit_idx) & mask) |
| 2520 | return false; |
| 2521 | return true; |
| 2522 | } |
| 2523 | |
| 2524 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2525 | u16 port, u16 len) |
| 2526 | { |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2527 | if (ctxt->perm_ok) |
| 2528 | return true; |
| 2529 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2530 | if (emulator_bad_iopl(ctxt)) |
| 2531 | if (!emulator_io_port_access_allowed(ctxt, port, len)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2532 | return false; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 2533 | |
| 2534 | ctxt->perm_ok = true; |
| 2535 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2536 | return true; |
| 2537 | } |
| 2538 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2539 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2540 | struct tss_segment_16 *tss) |
| 2541 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2542 | tss->ip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2543 | tss->flag = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2544 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2545 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2546 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2547 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2548 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2549 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2550 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); |
| 2551 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2552 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2553 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2554 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2555 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2556 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2557 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2561 | struct tss_segment_16 *tss) |
| 2562 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2563 | int ret; |
| 2564 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2565 | ctxt->_eip = tss->ip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2566 | ctxt->eflags = tss->flag | 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2567 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
| 2568 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; |
| 2569 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; |
| 2570 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; |
| 2571 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; |
| 2572 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; |
| 2573 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; |
| 2574 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* |
| 2577 | * SDM says that segment selectors are loaded before segment |
| 2578 | * descriptors |
| 2579 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2580 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
| 2581 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2582 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2583 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2584 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2585 | |
| 2586 | /* |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2587 | * Now load segment descriptors. If fault happens at this stage |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2588 | * it is handled in a context of new task |
| 2589 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2590 | ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2591 | if (ret != X86EMUL_CONTINUE) |
| 2592 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2593 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2594 | if (ret != X86EMUL_CONTINUE) |
| 2595 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2596 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2597 | if (ret != X86EMUL_CONTINUE) |
| 2598 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2599 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2600 | if (ret != X86EMUL_CONTINUE) |
| 2601 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2602 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2603 | if (ret != X86EMUL_CONTINUE) |
| 2604 | return ret; |
| 2605 | |
| 2606 | return X86EMUL_CONTINUE; |
| 2607 | } |
| 2608 | |
| 2609 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2610 | u16 tss_selector, u16 old_tss_sel, |
| 2611 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2612 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2613 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2614 | struct tss_segment_16 tss_seg; |
| 2615 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2616 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2617 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2618 | 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] | 2619 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2620 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2621 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2622 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2623 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2624 | save_state_to_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2625 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2626 | 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] | 2627 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2628 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2629 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2630 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2631 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2632 | 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] | 2633 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2634 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2635 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2636 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2637 | |
| 2638 | if (old_tss_sel != 0xffff) { |
| 2639 | tss_seg.prev_task_link = old_tss_sel; |
| 2640 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2641 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2642 | &tss_seg.prev_task_link, |
| 2643 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2644 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2645 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2646 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2647 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2650 | return load_state_from_tss16(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2654 | struct tss_segment_32 *tss) |
| 2655 | { |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2656 | tss->cr3 = ctxt->ops->get_cr(ctxt, 3); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2657 | tss->eip = ctxt->_eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2658 | tss->eflags = ctxt->eflags; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2659 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 2660 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); |
| 2661 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); |
| 2662 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); |
| 2663 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); |
| 2664 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); |
| 2665 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); |
| 2666 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2667 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2668 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
| 2669 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
| 2670 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); |
| 2671 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); |
| 2672 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); |
| 2673 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); |
| 2674 | tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2678 | struct tss_segment_32 *tss) |
| 2679 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2680 | int ret; |
| 2681 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2682 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 2683 | return emulate_gp(ctxt, 0); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2684 | ctxt->_eip = tss->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2685 | ctxt->eflags = tss->eflags | 2; |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2686 | |
| 2687 | /* General purpose registers */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2688 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
| 2689 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; |
| 2690 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; |
| 2691 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; |
| 2692 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; |
| 2693 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; |
| 2694 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; |
| 2695 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2696 | |
| 2697 | /* |
| 2698 | * SDM says that segment selectors are loaded before segment |
| 2699 | * descriptors |
| 2700 | */ |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2701 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2702 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); |
| 2703 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); |
| 2704 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); |
| 2705 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); |
| 2706 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); |
| 2707 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2708 | |
| 2709 | /* |
Kevin Wolf | 4cee479 | 2012-02-08 14:34:41 +0100 | [diff] [blame] | 2710 | * If we're switching between Protected Mode and VM86, we need to make |
| 2711 | * sure to update the mode before loading the segment descriptors so |
| 2712 | * that the selectors are interpreted correctly. |
| 2713 | * |
| 2714 | * Need to get rflags to the vcpu struct immediately because it |
| 2715 | * influences the CPL which is checked at least when loading the segment |
| 2716 | * descriptors and when pushing an error code to the new kernel stack. |
| 2717 | * |
| 2718 | * TODO Introduce a separate ctxt->ops->set_cpl callback |
| 2719 | */ |
| 2720 | if (ctxt->eflags & X86_EFLAGS_VM) |
| 2721 | ctxt->mode = X86EMUL_MODE_VM86; |
| 2722 | else |
| 2723 | ctxt->mode = X86EMUL_MODE_PROT32; |
| 2724 | |
| 2725 | ctxt->ops->set_rflags(ctxt, ctxt->eflags); |
| 2726 | |
| 2727 | /* |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2728 | * Now load segment descriptors. If fault happenes at this stage |
| 2729 | * it is handled in a context of new task |
| 2730 | */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2731 | ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2732 | if (ret != X86EMUL_CONTINUE) |
| 2733 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2734 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2735 | if (ret != X86EMUL_CONTINUE) |
| 2736 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2737 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2738 | if (ret != X86EMUL_CONTINUE) |
| 2739 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2740 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2741 | if (ret != X86EMUL_CONTINUE) |
| 2742 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2743 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2744 | if (ret != X86EMUL_CONTINUE) |
| 2745 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2746 | ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2747 | if (ret != X86EMUL_CONTINUE) |
| 2748 | return ret; |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2749 | ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2750 | if (ret != X86EMUL_CONTINUE) |
| 2751 | return ret; |
| 2752 | |
| 2753 | return X86EMUL_CONTINUE; |
| 2754 | } |
| 2755 | |
| 2756 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2757 | u16 tss_selector, u16 old_tss_sel, |
| 2758 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2759 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2760 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2761 | struct tss_segment_32 tss_seg; |
| 2762 | int ret; |
Avi Kivity | bcc55cb | 2010-11-22 17:53:22 +0200 | [diff] [blame] | 2763 | u32 new_tss_base = get_desc_base(new_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2764 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2765 | 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] | 2766 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2767 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2768 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2769 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2770 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2771 | save_state_to_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2772 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2773 | 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] | 2774 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2775 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2776 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2777 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2778 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2779 | 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] | 2780 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2781 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2782 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2783 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2784 | |
| 2785 | if (old_tss_sel != 0xffff) { |
| 2786 | tss_seg.prev_task_link = old_tss_sel; |
| 2787 | |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2788 | ret = ops->write_std(ctxt, new_tss_base, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2789 | &tss_seg.prev_task_link, |
| 2790 | sizeof tss_seg.prev_task_link, |
Avi Kivity | 0f65dd7 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2791 | &ctxt->exception); |
Avi Kivity | db297e3 | 2010-11-22 17:53:24 +0200 | [diff] [blame] | 2792 | if (ret != X86EMUL_CONTINUE) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2793 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2794 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2797 | return load_state_from_tss32(ctxt, &tss_seg); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2801 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2802 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2803 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 2804 | const struct x86_emulate_ops *ops = ctxt->ops; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2805 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2806 | int ret; |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 2807 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2808 | ulong old_tss_base = |
Avi Kivity | 4bff1e86 | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2809 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2810 | u32 desc_limit; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2811 | ulong desc_addr; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2812 | |
| 2813 | /* FIXME: old_tss_base == ~0 ? */ |
| 2814 | |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2815 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2816 | if (ret != X86EMUL_CONTINUE) |
| 2817 | return ret; |
Avi Kivity | e919464 | 2012-06-13 16:29:39 +0300 | [diff] [blame] | 2818 | 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] | 2819 | if (ret != X86EMUL_CONTINUE) |
| 2820 | return ret; |
| 2821 | |
| 2822 | /* FIXME: check that next_tss_desc is tss */ |
| 2823 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2824 | /* |
| 2825 | * Check privileges. The three cases are task switch caused by... |
| 2826 | * |
| 2827 | * 1. jmp/call/int to task gate: Check against DPL of the task gate |
| 2828 | * 2. Exception/IRQ/iret: No check is performed |
Guo Chao | fc05868 | 2012-06-28 15:19:51 +0800 | [diff] [blame] | 2829 | * 3. jmp/call to TSS: Check against DPL of the TSS |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2830 | */ |
| 2831 | if (reason == TASK_SWITCH_GATE) { |
| 2832 | if (idt_index != -1) { |
| 2833 | /* Software interrupts */ |
| 2834 | struct desc_struct task_gate_desc; |
| 2835 | int dpl; |
| 2836 | |
| 2837 | ret = read_interrupt_descriptor(ctxt, idt_index, |
| 2838 | &task_gate_desc); |
| 2839 | if (ret != X86EMUL_CONTINUE) |
| 2840 | return ret; |
| 2841 | |
| 2842 | dpl = task_gate_desc.dpl; |
| 2843 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2844 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); |
| 2845 | } |
| 2846 | } else if (reason != TASK_SWITCH_IRET) { |
| 2847 | int dpl = next_tss_desc.dpl; |
| 2848 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) |
| 2849 | return emulate_gp(ctxt, tss_selector); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2852 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2853 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2854 | if (!next_tss_desc.p || |
| 2855 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2856 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2857 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2858 | return X86EMUL_PROPAGATE_FAULT; |
| 2859 | } |
| 2860 | |
| 2861 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2862 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2863 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | if (reason == TASK_SWITCH_IRET) |
| 2867 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2868 | |
| 2869 | /* 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] | 2870 | note that old_tss_sel is not used after this point */ |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2871 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2872 | old_tss_sel = 0xffff; |
| 2873 | |
| 2874 | if (next_tss_desc.type & 8) |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2875 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2876 | old_tss_base, &next_tss_desc); |
| 2877 | else |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2878 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2879 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2880 | if (ret != X86EMUL_CONTINUE) |
| 2881 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2882 | |
| 2883 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2884 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2885 | |
| 2886 | if (reason != TASK_SWITCH_IRET) { |
| 2887 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 2888 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2889 | } |
| 2890 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 2891 | 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] | 2892 | 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] | 2893 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2894 | if (has_error_code) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2895 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2896 | ctxt->lock_prefix = 0; |
| 2897 | ctxt->src.val = (unsigned long) error_code; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 2898 | ret = em_push(ctxt); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2901 | return ret; |
| 2902 | } |
| 2903 | |
| 2904 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2905 | u16 tss_selector, int idt_index, int reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2906 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2907 | { |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2908 | int rc; |
| 2909 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2910 | invalidate_registers(ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2911 | ctxt->_eip = ctxt->eip; |
| 2912 | ctxt->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2913 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2914 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2915 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2916 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2917 | if (rc == X86EMUL_CONTINUE) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2918 | ctxt->eip = ctxt->_eip; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2919 | writeback_registers(ctxt); |
| 2920 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2921 | |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 2922 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 2925 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
| 2926 | struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2927 | { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 2928 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2929 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 2930 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
| 2931 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2932 | } |
| 2933 | |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2934 | static int em_das(struct x86_emulate_ctxt *ctxt) |
| 2935 | { |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2936 | u8 al, old_al; |
| 2937 | bool af, cf, old_cf; |
| 2938 | |
| 2939 | cf = ctxt->eflags & X86_EFLAGS_CF; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2940 | al = ctxt->dst.val; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2941 | |
| 2942 | old_al = al; |
| 2943 | old_cf = cf; |
| 2944 | cf = false; |
| 2945 | af = ctxt->eflags & X86_EFLAGS_AF; |
| 2946 | if ((al & 0x0f) > 9 || af) { |
| 2947 | al -= 6; |
| 2948 | cf = old_cf | (al >= 250); |
| 2949 | af = true; |
| 2950 | } else { |
| 2951 | af = false; |
| 2952 | } |
| 2953 | if (old_al > 0x99 || old_cf) { |
| 2954 | al -= 0x60; |
| 2955 | cf = true; |
| 2956 | } |
| 2957 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2958 | ctxt->dst.val = al; |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2959 | /* Set PF, ZF, SF */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 2960 | ctxt->src.type = OP_IMM; |
| 2961 | ctxt->src.val = 0; |
| 2962 | ctxt->src.bytes = 1; |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 2963 | emulate_2op_SrcV(ctxt, "or"); |
Avi Kivity | 7af04fc | 2010-08-18 14:16:35 +0300 | [diff] [blame] | 2964 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
| 2965 | if (cf) |
| 2966 | ctxt->eflags |= X86_EFLAGS_CF; |
| 2967 | if (af) |
| 2968 | ctxt->eflags |= X86_EFLAGS_AF; |
| 2969 | return X86EMUL_CONTINUE; |
| 2970 | } |
| 2971 | |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 2972 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
| 2973 | { |
| 2974 | u8 al = ctxt->dst.val & 0xff; |
| 2975 | u8 ah = (ctxt->dst.val >> 8) & 0xff; |
| 2976 | |
| 2977 | al = (al + (ah * ctxt->src.val)) & 0xff; |
| 2978 | |
| 2979 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; |
| 2980 | |
| 2981 | ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF); |
| 2982 | |
| 2983 | if (!al) |
| 2984 | ctxt->eflags |= X86_EFLAGS_ZF; |
| 2985 | if (!(al & 1)) |
| 2986 | ctxt->eflags |= X86_EFLAGS_PF; |
| 2987 | if (al & 0x80) |
| 2988 | ctxt->eflags |= X86_EFLAGS_SF; |
| 2989 | |
| 2990 | return X86EMUL_CONTINUE; |
| 2991 | } |
| 2992 | |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 2993 | static int em_call(struct x86_emulate_ctxt *ctxt) |
| 2994 | { |
| 2995 | long rel = ctxt->src.val; |
| 2996 | |
| 2997 | ctxt->src.val = (unsigned long)ctxt->_eip; |
| 2998 | jmp_rel(ctxt, rel); |
| 2999 | return em_push(ctxt); |
| 3000 | } |
| 3001 | |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3002 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
| 3003 | { |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3004 | u16 sel, old_cs; |
| 3005 | ulong old_eip; |
| 3006 | int rc; |
| 3007 | |
Avi Kivity | 1aa3661 | 2011-04-27 13:20:30 +0300 | [diff] [blame] | 3008 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3009 | old_eip = ctxt->_eip; |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3010 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3011 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 3012 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3013 | return X86EMUL_CONTINUE; |
| 3014 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3015 | ctxt->_eip = 0; |
| 3016 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3017 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3018 | ctxt->src.val = old_cs; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3019 | rc = em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3020 | if (rc != X86EMUL_CONTINUE) |
| 3021 | return rc; |
| 3022 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3023 | ctxt->src.val = old_eip; |
Takuya Yoshikawa | 4487b3b | 2011-04-13 00:31:23 +0900 | [diff] [blame] | 3024 | return em_push(ctxt); |
Avi Kivity | 0ef753b | 2010-08-18 14:51:45 +0300 | [diff] [blame] | 3025 | } |
| 3026 | |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3027 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
| 3028 | { |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3029 | int rc; |
| 3030 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3031 | ctxt->dst.type = OP_REG; |
| 3032 | ctxt->dst.addr.reg = &ctxt->_eip; |
| 3033 | ctxt->dst.bytes = ctxt->op_bytes; |
| 3034 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3035 | if (rc != X86EMUL_CONTINUE) |
| 3036 | return rc; |
Avi Kivity | 5ad105e | 2012-08-19 14:34:31 +0300 | [diff] [blame] | 3037 | rsp_increment(ctxt, ctxt->src.val); |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3038 | return X86EMUL_CONTINUE; |
| 3039 | } |
| 3040 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3041 | FASTOP2(add); |
| 3042 | FASTOP2(or); |
| 3043 | FASTOP2(adc); |
| 3044 | FASTOP2(sbb); |
| 3045 | FASTOP2(and); |
| 3046 | FASTOP2(sub); |
| 3047 | FASTOP2(xor); |
| 3048 | FASTOP2(cmp); |
| 3049 | FASTOP2(test); |
Takuya Yoshikawa | 9f21ca5 | 2011-05-29 21:57:53 +0900 | [diff] [blame] | 3050 | |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 3051 | FASTOP3WCL(shld); |
| 3052 | FASTOP3WCL(shrd); |
| 3053 | |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3054 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
| 3055 | { |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3056 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3057 | ctxt->src.val = ctxt->dst.val; |
| 3058 | write_register_operand(&ctxt->src); |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3059 | |
| 3060 | /* Write back the memory destination with implicit LOCK prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3061 | ctxt->dst.val = ctxt->src.orig_val; |
| 3062 | ctxt->lock_prefix = 1; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 3063 | return X86EMUL_CONTINUE; |
| 3064 | } |
| 3065 | |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 3066 | static int em_imul(struct x86_emulate_ctxt *ctxt) |
| 3067 | { |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 3068 | emulate_2op_SrcV_nobyte(ctxt, "imul"); |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 3069 | return X86EMUL_CONTINUE; |
| 3070 | } |
| 3071 | |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3072 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
| 3073 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3074 | ctxt->dst.val = ctxt->src2.val; |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 3075 | return em_imul(ctxt); |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3076 | } |
| 3077 | |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3078 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
| 3079 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3080 | ctxt->dst.type = OP_REG; |
| 3081 | ctxt->dst.bytes = ctxt->src.bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3082 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3083 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3084 | |
| 3085 | return X86EMUL_CONTINUE; |
| 3086 | } |
| 3087 | |
Avi Kivity | 48bb5d3c4 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3088 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3089 | { |
Avi Kivity | 48bb5d3c4 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3090 | u64 tsc = 0; |
| 3091 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3092 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3093 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
| 3094 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; |
Avi Kivity | 48bb5d3c4 | 2010-08-18 18:54:34 +0300 | [diff] [blame] | 3095 | return X86EMUL_CONTINUE; |
| 3096 | } |
| 3097 | |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3098 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3099 | { |
| 3100 | u64 pmc; |
| 3101 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3102 | 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] | 3103 | return emulate_gp(ctxt, 0); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3104 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
| 3105 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3106 | return X86EMUL_CONTINUE; |
| 3107 | } |
| 3108 | |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3109 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
| 3110 | { |
Stefan Hajnoczi | 49597d8 | 2012-04-09 18:40:00 +0300 | [diff] [blame] | 3111 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3112 | return X86EMUL_CONTINUE; |
| 3113 | } |
| 3114 | |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3115 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3116 | { |
| 3117 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) |
| 3118 | return emulate_gp(ctxt, 0); |
| 3119 | |
| 3120 | /* Disable writeback. */ |
| 3121 | ctxt->dst.type = OP_NONE; |
| 3122 | return X86EMUL_CONTINUE; |
| 3123 | } |
| 3124 | |
| 3125 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3126 | { |
| 3127 | unsigned long val; |
| 3128 | |
| 3129 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3130 | val = ctxt->src.val & ~0ULL; |
| 3131 | else |
| 3132 | val = ctxt->src.val & ~0U; |
| 3133 | |
| 3134 | /* #UD condition is already handled. */ |
| 3135 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) |
| 3136 | return emulate_gp(ctxt, 0); |
| 3137 | |
| 3138 | /* Disable writeback. */ |
| 3139 | ctxt->dst.type = OP_NONE; |
| 3140 | return X86EMUL_CONTINUE; |
| 3141 | } |
| 3142 | |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3143 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
| 3144 | { |
| 3145 | u64 msr_data; |
| 3146 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3147 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
| 3148 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); |
| 3149 | 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] | 3150 | return emulate_gp(ctxt, 0); |
| 3151 | |
| 3152 | return X86EMUL_CONTINUE; |
| 3153 | } |
| 3154 | |
| 3155 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) |
| 3156 | { |
| 3157 | u64 msr_data; |
| 3158 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3159 | 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] | 3160 | return emulate_gp(ctxt, 0); |
| 3161 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3162 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
| 3163 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3164 | return X86EMUL_CONTINUE; |
| 3165 | } |
| 3166 | |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3167 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
| 3168 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3169 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3170 | return emulate_ud(ctxt); |
| 3171 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3172 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3173 | return X86EMUL_CONTINUE; |
| 3174 | } |
| 3175 | |
| 3176 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) |
| 3177 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3178 | u16 sel = ctxt->src.val; |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3179 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3180 | 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] | 3181 | return emulate_ud(ctxt); |
| 3182 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3183 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3184 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
| 3185 | |
| 3186 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3187 | ctxt->dst.type = OP_NONE; |
| 3188 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3189 | } |
| 3190 | |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3191 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
| 3192 | { |
| 3193 | u16 sel = ctxt->src.val; |
| 3194 | |
| 3195 | /* Disable writeback. */ |
| 3196 | ctxt->dst.type = OP_NONE; |
| 3197 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); |
| 3198 | } |
| 3199 | |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3200 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
| 3201 | { |
| 3202 | u16 sel = ctxt->src.val; |
| 3203 | |
| 3204 | /* Disable writeback. */ |
| 3205 | ctxt->dst.type = OP_NONE; |
| 3206 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); |
| 3207 | } |
| 3208 | |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3209 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
| 3210 | { |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3211 | int rc; |
| 3212 | ulong linear; |
| 3213 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3214 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
Avi Kivity | 9fa088f | 2011-03-31 18:54:30 +0200 | [diff] [blame] | 3215 | if (rc == X86EMUL_CONTINUE) |
Avi Kivity | 3cb16fe | 2011-04-20 15:38:44 +0300 | [diff] [blame] | 3216 | ctxt->ops->invlpg(ctxt, linear); |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3217 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3218 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 3850391 | 2011-03-31 18:48:09 +0200 | [diff] [blame] | 3219 | return X86EMUL_CONTINUE; |
| 3220 | } |
| 3221 | |
Avi Kivity | 2d04a05 | 2011-04-20 15:32:49 +0300 | [diff] [blame] | 3222 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
| 3223 | { |
| 3224 | ulong cr0; |
| 3225 | |
| 3226 | cr0 = ctxt->ops->get_cr(ctxt, 0); |
| 3227 | cr0 &= ~X86_CR0_TS; |
| 3228 | ctxt->ops->set_cr(ctxt, 0, cr0); |
| 3229 | return X86EMUL_CONTINUE; |
| 3230 | } |
| 3231 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3232 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
| 3233 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3234 | int rc; |
| 3235 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3236 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3237 | return X86EMUL_UNHANDLEABLE; |
| 3238 | |
| 3239 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3240 | if (rc != X86EMUL_CONTINUE) |
| 3241 | return rc; |
| 3242 | |
| 3243 | /* Let the processor re-execute the fixed hypercall */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3244 | ctxt->_eip = ctxt->eip; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3245 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3246 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3247 | return X86EMUL_CONTINUE; |
| 3248 | } |
| 3249 | |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3250 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
| 3251 | void (*get)(struct x86_emulate_ctxt *ctxt, |
| 3252 | struct desc_ptr *ptr)) |
| 3253 | { |
| 3254 | struct desc_ptr desc_ptr; |
| 3255 | |
| 3256 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3257 | ctxt->op_bytes = 8; |
| 3258 | get(ctxt, &desc_ptr); |
| 3259 | if (ctxt->op_bytes == 2) { |
| 3260 | ctxt->op_bytes = 4; |
| 3261 | desc_ptr.address &= 0x00ffffff; |
| 3262 | } |
| 3263 | /* Disable writeback. */ |
| 3264 | ctxt->dst.type = OP_NONE; |
| 3265 | return segmented_write(ctxt, ctxt->dst.addr.mem, |
| 3266 | &desc_ptr, 2 + ctxt->op_bytes); |
| 3267 | } |
| 3268 | |
| 3269 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) |
| 3270 | { |
| 3271 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); |
| 3272 | } |
| 3273 | |
| 3274 | static int em_sidt(struct x86_emulate_ctxt *ctxt) |
| 3275 | { |
| 3276 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); |
| 3277 | } |
| 3278 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3279 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
| 3280 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3281 | struct desc_ptr desc_ptr; |
| 3282 | int rc; |
| 3283 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3284 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3285 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3286 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3287 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3288 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3289 | if (rc != X86EMUL_CONTINUE) |
| 3290 | return rc; |
| 3291 | ctxt->ops->set_gdt(ctxt, &desc_ptr); |
| 3292 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3293 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3294 | return X86EMUL_CONTINUE; |
| 3295 | } |
| 3296 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3297 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3298 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3299 | int rc; |
| 3300 | |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3301 | rc = ctxt->ops->fix_hypercall(ctxt); |
| 3302 | |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3303 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3304 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3305 | return rc; |
| 3306 | } |
| 3307 | |
| 3308 | static int em_lidt(struct x86_emulate_ctxt *ctxt) |
| 3309 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3310 | struct desc_ptr desc_ptr; |
| 3311 | int rc; |
| 3312 | |
Avi Kivity | 510425f | 2012-06-07 17:04:36 +0300 | [diff] [blame] | 3313 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 3314 | ctxt->op_bytes = 8; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3315 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
Takuya Yoshikawa | 509cf9f | 2011-05-02 02:25:07 +0900 | [diff] [blame] | 3316 | &desc_ptr.size, &desc_ptr.address, |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3317 | ctxt->op_bytes); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3318 | if (rc != X86EMUL_CONTINUE) |
| 3319 | return rc; |
| 3320 | ctxt->ops->set_idt(ctxt, &desc_ptr); |
| 3321 | /* Disable writeback. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3322 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3323 | return X86EMUL_CONTINUE; |
| 3324 | } |
| 3325 | |
| 3326 | static int em_smsw(struct x86_emulate_ctxt *ctxt) |
| 3327 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3328 | ctxt->dst.bytes = 2; |
| 3329 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3330 | return X86EMUL_CONTINUE; |
| 3331 | } |
| 3332 | |
| 3333 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) |
| 3334 | { |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3335 | 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] | 3336 | | (ctxt->src.val & 0x0f)); |
| 3337 | ctxt->dst.type = OP_NONE; |
Avi Kivity | 26d05cc | 2011-04-21 12:07:59 +0300 | [diff] [blame] | 3338 | return X86EMUL_CONTINUE; |
| 3339 | } |
| 3340 | |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3341 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
| 3342 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3343 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
| 3344 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3345 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
| 3346 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3347 | |
| 3348 | return X86EMUL_CONTINUE; |
| 3349 | } |
| 3350 | |
| 3351 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) |
| 3352 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3353 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3354 | jmp_rel(ctxt, ctxt->src.val); |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3355 | |
| 3356 | return X86EMUL_CONTINUE; |
| 3357 | } |
| 3358 | |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3359 | static int em_in(struct x86_emulate_ctxt *ctxt) |
| 3360 | { |
| 3361 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, |
| 3362 | &ctxt->dst.val)) |
| 3363 | return X86EMUL_IO_NEEDED; |
| 3364 | |
| 3365 | return X86EMUL_CONTINUE; |
| 3366 | } |
| 3367 | |
| 3368 | static int em_out(struct x86_emulate_ctxt *ctxt) |
| 3369 | { |
| 3370 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, |
| 3371 | &ctxt->src.val, 1); |
| 3372 | /* Disable writeback. */ |
| 3373 | ctxt->dst.type = OP_NONE; |
| 3374 | return X86EMUL_CONTINUE; |
| 3375 | } |
| 3376 | |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3377 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
| 3378 | { |
| 3379 | if (emulator_bad_iopl(ctxt)) |
| 3380 | return emulate_gp(ctxt, 0); |
| 3381 | |
| 3382 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3383 | return X86EMUL_CONTINUE; |
| 3384 | } |
| 3385 | |
| 3386 | static int em_sti(struct x86_emulate_ctxt *ctxt) |
| 3387 | { |
| 3388 | if (emulator_bad_iopl(ctxt)) |
| 3389 | return emulate_gp(ctxt, 0); |
| 3390 | |
| 3391 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
| 3392 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3393 | return X86EMUL_CONTINUE; |
| 3394 | } |
| 3395 | |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3396 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
| 3397 | { |
| 3398 | u32 eax, ebx, ecx, edx; |
| 3399 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3400 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
| 3401 | ecx = reg_read(ctxt, VCPU_REGS_RCX); |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3402 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3403 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
| 3404 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; |
| 3405 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; |
| 3406 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; |
Avi Kivity | 6d6eede | 2012-06-07 14:11:36 +0300 | [diff] [blame] | 3407 | return X86EMUL_CONTINUE; |
| 3408 | } |
| 3409 | |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3410 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
| 3411 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3412 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
| 3413 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3414 | return X86EMUL_CONTINUE; |
| 3415 | } |
| 3416 | |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 3417 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
| 3418 | { |
| 3419 | switch (ctxt->op_bytes) { |
| 3420 | #ifdef CONFIG_X86_64 |
| 3421 | case 8: |
| 3422 | asm("bswap %0" : "+r"(ctxt->dst.val)); |
| 3423 | break; |
| 3424 | #endif |
| 3425 | default: |
| 3426 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); |
| 3427 | break; |
| 3428 | } |
| 3429 | return X86EMUL_CONTINUE; |
| 3430 | } |
| 3431 | |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3432 | static bool valid_cr(int nr) |
| 3433 | { |
| 3434 | switch (nr) { |
| 3435 | case 0: |
| 3436 | case 2 ... 4: |
| 3437 | case 8: |
| 3438 | return true; |
| 3439 | default: |
| 3440 | return false; |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) |
| 3445 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3446 | if (!valid_cr(ctxt->modrm_reg)) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3447 | return emulate_ud(ctxt); |
| 3448 | |
| 3449 | return X86EMUL_CONTINUE; |
| 3450 | } |
| 3451 | |
| 3452 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) |
| 3453 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3454 | u64 new_val = ctxt->src.val64; |
| 3455 | int cr = ctxt->modrm_reg; |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3456 | u64 efer = 0; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3457 | |
| 3458 | static u64 cr_reserved_bits[] = { |
| 3459 | 0xffffffff00000000ULL, |
| 3460 | 0, 0, 0, /* CR3 checked later */ |
| 3461 | CR4_RESERVED_BITS, |
| 3462 | 0, 0, 0, |
| 3463 | CR8_RESERVED_BITS, |
| 3464 | }; |
| 3465 | |
| 3466 | if (!valid_cr(cr)) |
| 3467 | return emulate_ud(ctxt); |
| 3468 | |
| 3469 | if (new_val & cr_reserved_bits[cr]) |
| 3470 | return emulate_gp(ctxt, 0); |
| 3471 | |
| 3472 | switch (cr) { |
| 3473 | case 0: { |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3474 | u64 cr4; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3475 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
| 3476 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) |
| 3477 | return emulate_gp(ctxt, 0); |
| 3478 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3479 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
| 3480 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3481 | |
| 3482 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && |
| 3483 | !(cr4 & X86_CR4_PAE)) |
| 3484 | return emulate_gp(ctxt, 0); |
| 3485 | |
| 3486 | break; |
| 3487 | } |
| 3488 | case 3: { |
| 3489 | u64 rsvd = 0; |
| 3490 | |
Avi Kivity | c2ad2bb | 2011-04-20 15:21:35 +0300 | [diff] [blame] | 3491 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
| 3492 | if (efer & EFER_LMA) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3493 | rsvd = CR3_L_MODE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3494 | else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3495 | rsvd = CR3_PAE_RESERVED_BITS; |
Avi Kivity | fd72c41 | 2011-04-20 15:24:32 +0300 | [diff] [blame] | 3496 | else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3497 | rsvd = CR3_NONPAE_RESERVED_BITS; |
| 3498 | |
| 3499 | if (new_val & rsvd) |
| 3500 | return emulate_gp(ctxt, 0); |
| 3501 | |
| 3502 | break; |
| 3503 | } |
| 3504 | case 4: { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3505 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3506 | |
| 3507 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) |
| 3508 | return emulate_gp(ctxt, 0); |
| 3509 | |
| 3510 | break; |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | return X86EMUL_CONTINUE; |
| 3515 | } |
| 3516 | |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3517 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
| 3518 | { |
| 3519 | unsigned long dr7; |
| 3520 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3521 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3522 | |
| 3523 | /* Check if DR7.Global_Enable is set */ |
| 3524 | return dr7 & (1 << 13); |
| 3525 | } |
| 3526 | |
| 3527 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) |
| 3528 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3529 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3530 | u64 cr4; |
| 3531 | |
| 3532 | if (dr > 7) |
| 3533 | return emulate_ud(ctxt); |
| 3534 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3535 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3536 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
| 3537 | return emulate_ud(ctxt); |
| 3538 | |
| 3539 | if (check_dr7_gd(ctxt)) |
| 3540 | return emulate_db(ctxt); |
| 3541 | |
| 3542 | return X86EMUL_CONTINUE; |
| 3543 | } |
| 3544 | |
| 3545 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) |
| 3546 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3547 | u64 new_val = ctxt->src.val64; |
| 3548 | int dr = ctxt->modrm_reg; |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3549 | |
| 3550 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) |
| 3551 | return emulate_gp(ctxt, 0); |
| 3552 | |
| 3553 | return check_dr_read(ctxt); |
| 3554 | } |
| 3555 | |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3556 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
| 3557 | { |
| 3558 | u64 efer; |
| 3559 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3560 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3561 | |
| 3562 | if (!(efer & EFER_SVME)) |
| 3563 | return emulate_ud(ctxt); |
| 3564 | |
| 3565 | return X86EMUL_CONTINUE; |
| 3566 | } |
| 3567 | |
| 3568 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) |
| 3569 | { |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3570 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3571 | |
| 3572 | /* Valid physical address? */ |
Randy Dunlap | d422444 | 2011-04-21 09:09:22 -0700 | [diff] [blame] | 3573 | if (rax & 0xffff000000000000ULL) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3574 | return emulate_gp(ctxt, 0); |
| 3575 | |
| 3576 | return check_svme(ctxt); |
| 3577 | } |
| 3578 | |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3579 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
| 3580 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3581 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3582 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3583 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3584 | return emulate_ud(ctxt); |
| 3585 | |
| 3586 | return X86EMUL_CONTINUE; |
| 3587 | } |
| 3588 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3589 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
| 3590 | { |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3591 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 3592 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3593 | |
Avi Kivity | 717746e | 2011-04-20 13:37:53 +0300 | [diff] [blame] | 3594 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3595 | (rcx > 3)) |
| 3596 | return emulate_gp(ctxt, 0); |
| 3597 | |
| 3598 | return X86EMUL_CONTINUE; |
| 3599 | } |
| 3600 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3601 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
| 3602 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3603 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
| 3604 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3605 | return emulate_gp(ctxt, 0); |
| 3606 | |
| 3607 | return X86EMUL_CONTINUE; |
| 3608 | } |
| 3609 | |
| 3610 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) |
| 3611 | { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 3612 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
| 3613 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3614 | return emulate_gp(ctxt, 0); |
| 3615 | |
| 3616 | return X86EMUL_CONTINUE; |
| 3617 | } |
| 3618 | |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3619 | #define D(_y) { .flags = (_y) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3620 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3621 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
| 3622 | .check_perm = (_p) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3623 | #define N D(0) |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3624 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3625 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
| 3626 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3627 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3628 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 3629 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 3630 | #define II(_f, _e, _i) \ |
| 3631 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 3632 | #define IIP(_f, _e, _i, _p) \ |
| 3633 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ |
| 3634 | .check_perm = (_p) } |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3635 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3636 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3637 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 3638 | #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] | 3639 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
Avi Kivity | f7857f3 | 2013-01-04 16:18:53 +0200 | [diff] [blame] | 3640 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3641 | #define I2bvIP(_f, _e, _i, _p) \ |
| 3642 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 3643 | |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3644 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
| 3645 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ |
| 3646 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3647 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3648 | static const struct opcode group7_rm1[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3649 | DI(SrcNone | Priv, monitor), |
| 3650 | DI(SrcNone | Priv, mwait), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3651 | N, N, N, N, N, N, |
| 3652 | }; |
| 3653 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3654 | static const struct opcode group7_rm3[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3655 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
| 3656 | II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall), |
| 3657 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), |
| 3658 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), |
| 3659 | DIP(SrcNone | Prot | Priv, stgi, check_svme), |
| 3660 | DIP(SrcNone | Prot | Priv, clgi, check_svme), |
| 3661 | DIP(SrcNone | Prot | Priv, skinit, check_svme), |
| 3662 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3663 | }; |
Avi Kivity | 6230f7f | 2010-08-26 18:34:55 +0300 | [diff] [blame] | 3664 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3665 | static const struct opcode group7_rm7[] = { |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3666 | N, |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3667 | DIP(SrcNone, rdtscp, check_rdtsc), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 3668 | N, N, N, N, N, N, |
| 3669 | }; |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 3670 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3671 | static const struct opcode group1[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3672 | F(Lock, em_add), |
| 3673 | F(Lock | PageTable, em_or), |
| 3674 | F(Lock, em_adc), |
| 3675 | F(Lock, em_sbb), |
| 3676 | F(Lock | PageTable, em_and), |
| 3677 | F(Lock, em_sub), |
| 3678 | F(Lock, em_xor), |
| 3679 | F(NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3680 | }; |
| 3681 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3682 | static const struct opcode group1A[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3683 | 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] | 3684 | }; |
| 3685 | |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3686 | static const struct opcode group2[] = { |
| 3687 | F(DstMem | ModRM, em_rol), |
| 3688 | F(DstMem | ModRM, em_ror), |
| 3689 | F(DstMem | ModRM, em_rcl), |
| 3690 | F(DstMem | ModRM, em_rcr), |
| 3691 | F(DstMem | ModRM, em_shl), |
| 3692 | F(DstMem | ModRM, em_shr), |
| 3693 | F(DstMem | ModRM, em_shl), |
| 3694 | F(DstMem | ModRM, em_sar), |
| 3695 | }; |
| 3696 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3697 | static const struct opcode group3[] = { |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3698 | F(DstMem | SrcImm | NoWrite, em_test), |
| 3699 | F(DstMem | SrcImm | NoWrite, em_test), |
Avi Kivity | 45a1467 | 2013-01-04 16:18:52 +0200 | [diff] [blame] | 3700 | F(DstMem | SrcNone | Lock, em_not), |
| 3701 | F(DstMem | SrcNone | Lock, em_neg), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3702 | I(SrcMem, em_mul_ex), |
| 3703 | I(SrcMem, em_imul_ex), |
| 3704 | I(SrcMem, em_div_ex), |
| 3705 | I(SrcMem, em_idiv_ex), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3706 | }; |
| 3707 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3708 | static const struct opcode group4[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3709 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
| 3710 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3711 | N, N, N, N, N, N, |
| 3712 | }; |
| 3713 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3714 | static const struct opcode group5[] = { |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3715 | F(DstMem | SrcNone | Lock, em_inc), |
| 3716 | F(DstMem | SrcNone | Lock, em_dec), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3717 | I(SrcMem | Stack, em_grp45), |
| 3718 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), |
| 3719 | I(SrcMem | Stack, em_grp45), |
| 3720 | I(SrcMemFAddr | ImplicitOps, em_grp45), |
| 3721 | I(SrcMem | Stack, em_grp45), N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3722 | }; |
| 3723 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3724 | static const struct opcode group6[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3725 | DI(Prot, sldt), |
| 3726 | DI(Prot, str), |
Avi Kivity | a14e579 | 2012-06-13 12:28:33 +0300 | [diff] [blame] | 3727 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
Avi Kivity | 8089000 | 2012-06-13 16:33:29 +0300 | [diff] [blame] | 3728 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3729 | N, N, N, N, |
| 3730 | }; |
| 3731 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3732 | static const struct group_dual group7 = { { |
Avi Kivity | 9605157 | 2012-06-10 17:21:18 +0300 | [diff] [blame] | 3733 | II(Mov | DstMem | Priv, em_sgdt, sgdt), |
| 3734 | II(Mov | DstMem | Priv, em_sidt, sidt), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3735 | II(SrcMem | Priv, em_lgdt, lgdt), |
| 3736 | II(SrcMem | Priv, em_lidt, lidt), |
| 3737 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3738 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3739 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3740 | }, { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3741 | I(SrcNone | Priv | VendorSpecific, em_vmcall), |
Avi Kivity | 5ef39c7 | 2011-04-21 12:21:50 +0300 | [diff] [blame] | 3742 | EXT(0, group7_rm1), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 3743 | N, EXT(0, group7_rm3), |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3744 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
| 3745 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), |
| 3746 | EXT(0, group7_rm7), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3747 | } }; |
| 3748 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3749 | static const struct opcode group8[] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3750 | N, N, N, N, |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 3751 | F(DstMem | SrcImmByte | NoWrite, em_bt), |
| 3752 | F(DstMem | SrcImmByte | Lock | PageTable, em_bts), |
| 3753 | F(DstMem | SrcImmByte | Lock, em_btr), |
| 3754 | F(DstMem | SrcImmByte | Lock | PageTable, em_btc), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3755 | }; |
| 3756 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3757 | static const struct group_dual group9 = { { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3758 | 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] | 3759 | }, { |
| 3760 | N, N, N, N, N, N, N, N, |
| 3761 | } }; |
| 3762 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3763 | static const struct opcode group11[] = { |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3764 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3765 | X7(D(Undefined)), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3766 | }; |
| 3767 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3768 | static const struct gprefix pfx_0f_6f_0f_7f = { |
Avi Kivity | e597175 | 2012-04-09 18:40:03 +0300 | [diff] [blame] | 3769 | 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] | 3770 | }; |
| 3771 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3772 | static const struct gprefix pfx_vmovntpx = { |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3773 | I(0, em_mov), N, N, N, |
| 3774 | }; |
| 3775 | |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3776 | static const struct escape escape_d9 = { { |
| 3777 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), |
| 3778 | }, { |
| 3779 | /* 0xC0 - 0xC7 */ |
| 3780 | N, N, N, N, N, N, N, N, |
| 3781 | /* 0xC8 - 0xCF */ |
| 3782 | N, N, N, N, N, N, N, N, |
| 3783 | /* 0xD0 - 0xC7 */ |
| 3784 | N, N, N, N, N, N, N, N, |
| 3785 | /* 0xD8 - 0xDF */ |
| 3786 | N, N, N, N, N, N, N, N, |
| 3787 | /* 0xE0 - 0xE7 */ |
| 3788 | N, N, N, N, N, N, N, N, |
| 3789 | /* 0xE8 - 0xEF */ |
| 3790 | N, N, N, N, N, N, N, N, |
| 3791 | /* 0xF0 - 0xF7 */ |
| 3792 | N, N, N, N, N, N, N, N, |
| 3793 | /* 0xF8 - 0xFF */ |
| 3794 | N, N, N, N, N, N, N, N, |
| 3795 | } }; |
| 3796 | |
| 3797 | static const struct escape escape_db = { { |
| 3798 | N, N, N, N, N, N, N, N, |
| 3799 | }, { |
| 3800 | /* 0xC0 - 0xC7 */ |
| 3801 | N, N, N, N, N, N, N, N, |
| 3802 | /* 0xC8 - 0xCF */ |
| 3803 | N, N, N, N, N, N, N, N, |
| 3804 | /* 0xD0 - 0xC7 */ |
| 3805 | N, N, N, N, N, N, N, N, |
| 3806 | /* 0xD8 - 0xDF */ |
| 3807 | N, N, N, N, N, N, N, N, |
| 3808 | /* 0xE0 - 0xE7 */ |
| 3809 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, |
| 3810 | /* 0xE8 - 0xEF */ |
| 3811 | N, N, N, N, N, N, N, N, |
| 3812 | /* 0xF0 - 0xF7 */ |
| 3813 | N, N, N, N, N, N, N, N, |
| 3814 | /* 0xF8 - 0xFF */ |
| 3815 | N, N, N, N, N, N, N, N, |
| 3816 | } }; |
| 3817 | |
| 3818 | static const struct escape escape_dd = { { |
| 3819 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), |
| 3820 | }, { |
| 3821 | /* 0xC0 - 0xC7 */ |
| 3822 | N, N, N, N, N, N, N, N, |
| 3823 | /* 0xC8 - 0xCF */ |
| 3824 | N, N, N, N, N, N, N, N, |
| 3825 | /* 0xD0 - 0xC7 */ |
| 3826 | N, N, N, N, N, N, N, N, |
| 3827 | /* 0xD8 - 0xDF */ |
| 3828 | N, N, N, N, N, N, N, N, |
| 3829 | /* 0xE0 - 0xE7 */ |
| 3830 | N, N, N, N, N, N, N, N, |
| 3831 | /* 0xE8 - 0xEF */ |
| 3832 | N, N, N, N, N, N, N, N, |
| 3833 | /* 0xF0 - 0xF7 */ |
| 3834 | N, N, N, N, N, N, N, N, |
| 3835 | /* 0xF8 - 0xFF */ |
| 3836 | N, N, N, N, N, N, N, N, |
| 3837 | } }; |
| 3838 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3839 | static const struct opcode opcode_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3840 | /* 0x00 - 0x07 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3841 | F6ALU(Lock, em_add), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3842 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
| 3843 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3844 | /* 0x08 - 0x0F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3845 | F6ALU(Lock | PageTable, em_or), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3846 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
| 3847 | N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3848 | /* 0x10 - 0x17 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3849 | F6ALU(Lock, em_adc), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3850 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
| 3851 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3852 | /* 0x18 - 0x1F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3853 | F6ALU(Lock, em_sbb), |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 3854 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
| 3855 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3856 | /* 0x20 - 0x27 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3857 | F6ALU(Lock | PageTable, em_and), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3858 | /* 0x28 - 0x2F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3859 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3860 | /* 0x30 - 0x37 */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3861 | F6ALU(Lock, em_xor), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3862 | /* 0x38 - 0x3F */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3863 | F6ALU(NoWrite, em_cmp), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3864 | /* 0x40 - 0x4F */ |
Avi Kivity | 95413dc | 2013-01-19 19:51:53 +0200 | [diff] [blame] | 3865 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3866 | /* 0x50 - 0x57 */ |
Avi Kivity | 6354038 | 2010-07-29 15:11:55 +0300 | [diff] [blame] | 3867 | X8(I(SrcReg | Stack, em_push)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3868 | /* 0x58 - 0x5F */ |
Takuya Yoshikawa | c54fe50 | 2011-04-23 18:49:40 +0900 | [diff] [blame] | 3869 | X8(I(DstReg | Stack, em_pop)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3870 | /* 0x60 - 0x67 */ |
Takuya Yoshikawa | b96a7fa | 2011-04-23 18:51:07 +0900 | [diff] [blame] | 3871 | I(ImplicitOps | Stack | No64, em_pusha), |
| 3872 | I(ImplicitOps | Stack | No64, em_popa), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3873 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 3874 | N, N, N, N, |
| 3875 | /* 0x68 - 0x6F */ |
Avi Kivity | d46164d | 2010-08-18 19:29:33 +0300 | [diff] [blame] | 3876 | I(SrcImm | Mov | Stack, em_push), |
| 3877 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), |
Avi Kivity | f3a1b9f | 2010-08-18 18:25:25 +0300 | [diff] [blame] | 3878 | I(SrcImmByte | Mov | Stack, em_push), |
| 3879 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 3880 | 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] | 3881 | 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] | 3882 | /* 0x70 - 0x7F */ |
| 3883 | X16(D(SrcImmByte)), |
| 3884 | /* 0x80 - 0x87 */ |
Takuya Yoshikawa | 1c2545b | 2012-04-30 17:46:31 +0900 | [diff] [blame] | 3885 | G(ByteOp | DstMem | SrcImm, group1), |
| 3886 | G(DstMem | SrcImm, group1), |
| 3887 | G(ByteOp | DstMem | SrcImm | No64, group1), |
| 3888 | G(DstMem | SrcImmByte, group1), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3889 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3890 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3891 | /* 0x88 - 0x8F */ |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3892 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3893 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3894 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
Takuya Yoshikawa | 1bd5f46 | 2011-05-29 22:01:33 +0900 | [diff] [blame] | 3895 | D(ModRM | SrcMem | NoAccess | DstReg), |
| 3896 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), |
| 3897 | G(0, group1A), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3898 | /* 0x90 - 0x97 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3899 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3900 | /* 0x98 - 0x9F */ |
Avi Kivity | 6142914 | 2010-08-19 15:13:00 +0300 | [diff] [blame] | 3901 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
Wei Yongjun | cc4feed | 2010-08-25 14:10:53 +0800 | [diff] [blame] | 3902 | I(SrcImmFAddr | No64, em_call_far), N, |
Takuya Yoshikawa | 62aaa2f | 2011-04-23 18:52:56 +0900 | [diff] [blame] | 3903 | II(ImplicitOps | Stack, em_pushf, pushf), |
Avi Kivity | 2dd7caa | 2012-06-11 13:09:07 +0300 | [diff] [blame] | 3904 | II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3905 | /* 0xA0 - 0xA7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3906 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 3907 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3908 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3909 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3910 | /* 0xA8 - 0xAF */ |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3911 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3912 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
| 3913 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), |
Avi Kivity | fb864fb | 2013-01-04 16:18:54 +0200 | [diff] [blame] | 3914 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3915 | /* 0xB0 - 0xB7 */ |
Avi Kivity | b9eac5f | 2010-08-03 14:46:56 +0300 | [diff] [blame] | 3916 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3917 | /* 0xB8 - 0xBF */ |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 3918 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3919 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3920 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
Avi Kivity | 40ece7c | 2010-08-18 15:12:09 +0300 | [diff] [blame] | 3921 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
Takuya Yoshikawa | ebda02c | 2011-05-29 22:00:22 +0900 | [diff] [blame] | 3922 | I(ImplicitOps | Stack, em_ret), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 3923 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
| 3924 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), |
Avi Kivity | a4d4a7c | 2010-08-03 15:05:46 +0300 | [diff] [blame] | 3925 | G(ByteOp, group11), G(0, group11), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3926 | /* 0xC8 - 0xCF */ |
Avi Kivity | 612e89f | 2012-06-12 20:03:23 +0300 | [diff] [blame] | 3927 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
| 3928 | N, I(ImplicitOps | Stack, em_ret_far), |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3929 | D(ImplicitOps), DI(SrcImmByte, intn), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3930 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3931 | /* 0xD0 - 0xD7 */ |
Avi Kivity | 007a3b5 | 2013-01-19 19:51:51 +0200 | [diff] [blame] | 3932 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
| 3933 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), |
Gleb Natapov | 7f66227 | 2012-12-10 11:42:30 +0200 | [diff] [blame] | 3934 | N, I(DstAcc | SrcImmByte | No64, em_aad), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3935 | /* 0xD8 - 0xDF */ |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 3936 | 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] | 3937 | /* 0xE0 - 0xE7 */ |
Takuya Yoshikawa | d06e03a | 2011-05-29 22:04:08 +0900 | [diff] [blame] | 3938 | X3(I(SrcImmByte, em_loop)), |
| 3939 | I(SrcImmByte, em_jcxz), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3940 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
| 3941 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3942 | /* 0xE8 - 0xEF */ |
Takuya Yoshikawa | d4ddafc | 2011-11-22 15:18:35 +0900 | [diff] [blame] | 3943 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3944 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 3945 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
| 3946 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3947 | /* 0xF0 - 0xF7 */ |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 3948 | N, DI(ImplicitOps, icebp), N, N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3949 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
| 3950 | G(ByteOp, group3), G(0, group3), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3951 | /* 0xF8 - 0xFF */ |
Takuya Yoshikawa | f411e6c | 2011-05-29 22:05:15 +0900 | [diff] [blame] | 3952 | D(ImplicitOps), D(ImplicitOps), |
| 3953 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3954 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
| 3955 | }; |
| 3956 | |
Mathias Krause | fd0a0d8 | 2012-08-30 01:30:15 +0200 | [diff] [blame] | 3957 | static const struct opcode twobyte_table[256] = { |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3958 | /* 0x00 - 0x0F */ |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 3959 | G(0, group6), GD(0, &group7), N, N, |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3960 | N, I(ImplicitOps | VendorSpecific, em_syscall), |
| 3961 | II(ImplicitOps | Priv, em_clts, clts), N, |
Avi Kivity | 3c6e276 | 2011-04-04 12:39:23 +0200 | [diff] [blame] | 3962 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3963 | N, D(ImplicitOps | ModRM), N, N, |
| 3964 | /* 0x10 - 0x1F */ |
| 3965 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, |
| 3966 | /* 0x20 - 0x2F */ |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 3967 | DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 3968 | DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read), |
Takuya Yoshikawa | bc00f8d | 2011-11-22 15:19:19 +0900 | [diff] [blame] | 3969 | IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write), |
| 3970 | 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] | 3971 | N, N, N, N, |
Avi Kivity | 3e114eb | 2012-04-09 18:40:01 +0300 | [diff] [blame] | 3972 | N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
| 3973 | N, N, N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3974 | /* 0x30 - 0x3F */ |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3975 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 3976 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
Takuya Yoshikawa | e1e210b | 2011-11-22 15:20:03 +0900 | [diff] [blame] | 3977 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
Avi Kivity | 222d21a | 2011-11-10 14:57:30 +0200 | [diff] [blame] | 3978 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 3979 | I(ImplicitOps | VendorSpecific, em_sysenter), |
| 3980 | I(ImplicitOps | Priv | VendorSpecific, em_sysexit), |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 3981 | N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 3982 | N, N, N, N, N, N, N, N, |
| 3983 | /* 0x40 - 0x4F */ |
| 3984 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
| 3985 | /* 0x50 - 0x5F */ |
| 3986 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 3987 | /* 0x60 - 0x6F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3988 | N, N, N, N, |
| 3989 | N, N, N, N, |
| 3990 | N, N, N, N, |
| 3991 | 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] | 3992 | /* 0x70 - 0x7F */ |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 3993 | N, N, N, N, |
| 3994 | N, N, N, N, |
| 3995 | N, N, N, N, |
| 3996 | 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] | 3997 | /* 0x80 - 0x8F */ |
| 3998 | X16(D(SrcImm)), |
| 3999 | /* 0x90 - 0x9F */ |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4000 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4001 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4002 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 4003 | II(ImplicitOps, em_cpuid, cpuid), |
| 4004 | F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4005 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
| 4006 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4007 | /* 0xA8 - 0xAF */ |
Avi Kivity | 1cd196e | 2011-09-13 10:45:51 +0300 | [diff] [blame] | 4008 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
Xiao Guangrong | d5ae7ce | 2011-09-22 16:53:46 +0800 | [diff] [blame] | 4009 | DI(ImplicitOps, rsm), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 4010 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
Avi Kivity | 0bdea06 | 2013-01-19 19:51:50 +0200 | [diff] [blame] | 4011 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
| 4012 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), |
Avi Kivity | 5c82aa2 | 2010-08-18 18:31:43 +0300 | [diff] [blame] | 4013 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4014 | /* 0xB0 - 0xB7 */ |
Takuya Yoshikawa | e940b5c | 2011-11-22 15:20:47 +0900 | [diff] [blame] | 4015 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4016 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 4017 | F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
Avi Kivity | d4b4325 | 2011-09-13 10:45:50 +0300 | [diff] [blame] | 4018 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
| 4019 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4020 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4021 | /* 0xB8 - 0xBF */ |
| 4022 | N, N, |
Takuya Yoshikawa | ce7faab | 2011-11-22 15:17:48 +0900 | [diff] [blame] | 4023 | G(BitOp, group8), |
Avi Kivity | 11c363b | 2013-01-19 19:51:54 +0200 | [diff] [blame^] | 4024 | F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), |
| 4025 | F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr), |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4026 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4027 | /* 0xC0 - 0xC7 */ |
Avi Kivity | 739ae40 | 2010-08-26 11:56:13 +0300 | [diff] [blame] | 4028 | D2bv(DstMem | SrcReg | ModRM | Lock), |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4029 | N, D(DstMem | SrcReg | ModRM | Mov), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4030 | N, N, N, GD(0, &group9), |
Avi Kivity | 9299836 | 2012-06-13 12:25:06 +0300 | [diff] [blame] | 4031 | /* 0xC8 - 0xCF */ |
| 4032 | X8(I(DstReg, em_bswap)), |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4033 | /* 0xD0 - 0xDF */ |
| 4034 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4035 | /* 0xE0 - 0xEF */ |
| 4036 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
| 4037 | /* 0xF0 - 0xFF */ |
| 4038 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
| 4039 | }; |
| 4040 | |
| 4041 | #undef D |
| 4042 | #undef N |
| 4043 | #undef G |
| 4044 | #undef GD |
| 4045 | #undef I |
Avi Kivity | aa97bb4 | 2010-01-20 18:09:23 +0200 | [diff] [blame] | 4046 | #undef GP |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4047 | #undef EXT |
Avi Kivity | 73fba5f | 2010-07-29 15:11:53 +0300 | [diff] [blame] | 4048 | |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4049 | #undef D2bv |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4050 | #undef D2bvIP |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4051 | #undef I2bv |
Takuya Yoshikawa | d7841a4 | 2011-11-22 15:16:54 +0900 | [diff] [blame] | 4052 | #undef I2bvIP |
Takuya Yoshikawa | d67fc27 | 2011-04-23 18:48:02 +0900 | [diff] [blame] | 4053 | #undef I6ALU |
Avi Kivity | 8d8f4e9 | 2010-08-26 11:56:06 +0300 | [diff] [blame] | 4054 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4055 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4056 | { |
| 4057 | unsigned size; |
| 4058 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4059 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4060 | if (size == 8) |
| 4061 | size = 4; |
| 4062 | return size; |
| 4063 | } |
| 4064 | |
| 4065 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4066 | unsigned size, bool sign_extension) |
| 4067 | { |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4068 | int rc = X86EMUL_CONTINUE; |
| 4069 | |
| 4070 | op->type = OP_IMM; |
| 4071 | op->bytes = size; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4072 | op->addr.mem.ea = ctxt->_eip; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4073 | /* NB. Immediates are sign-extended as necessary. */ |
| 4074 | switch (op->bytes) { |
| 4075 | case 1: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4076 | op->val = insn_fetch(s8, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4077 | break; |
| 4078 | case 2: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4079 | op->val = insn_fetch(s16, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4080 | break; |
| 4081 | case 4: |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4082 | op->val = insn_fetch(s32, ctxt); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4083 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4084 | case 8: |
| 4085 | op->val = insn_fetch(s64, ctxt); |
| 4086 | break; |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4087 | } |
| 4088 | if (!sign_extension) { |
| 4089 | switch (op->bytes) { |
| 4090 | case 1: |
| 4091 | op->val &= 0xff; |
| 4092 | break; |
| 4093 | case 2: |
| 4094 | op->val &= 0xffff; |
| 4095 | break; |
| 4096 | case 4: |
| 4097 | op->val &= 0xffffffff; |
| 4098 | break; |
| 4099 | } |
| 4100 | } |
| 4101 | done: |
| 4102 | return rc; |
| 4103 | } |
| 4104 | |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4105 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
| 4106 | unsigned d) |
| 4107 | { |
| 4108 | int rc = X86EMUL_CONTINUE; |
| 4109 | |
| 4110 | switch (d) { |
| 4111 | case OpReg: |
Avi Kivity | 2adb5ad | 2012-01-16 15:08:45 +0200 | [diff] [blame] | 4112 | decode_register_operand(ctxt, op); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4113 | break; |
| 4114 | case OpImmUByte: |
Avi Kivity | 608aabe | 2011-09-13 10:45:45 +0300 | [diff] [blame] | 4115 | rc = decode_imm(ctxt, op, 1, false); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4116 | break; |
| 4117 | case OpMem: |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4118 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4119 | mem_common: |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4120 | *op = ctxt->memop; |
| 4121 | ctxt->memopp = op; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4122 | if ((ctxt->d & BitOp) && op == &ctxt->dst) |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4123 | fetch_bit_operand(ctxt); |
| 4124 | op->orig_val = op->val; |
| 4125 | break; |
Avi Kivity | 41ddf97 | 2011-09-13 10:45:48 +0300 | [diff] [blame] | 4126 | case OpMem64: |
| 4127 | ctxt->memop.bytes = 8; |
| 4128 | goto mem_common; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4129 | case OpAcc: |
| 4130 | op->type = OP_REG; |
| 4131 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4132 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4133 | fetch_register_operand(op); |
| 4134 | op->orig_val = op->val; |
| 4135 | break; |
| 4136 | case OpDI: |
| 4137 | op->type = OP_MEM; |
| 4138 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4139 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4140 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4141 | op->addr.mem.seg = VCPU_SREG_ES; |
| 4142 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4143 | op->count = 1; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4144 | break; |
| 4145 | case OpDX: |
| 4146 | op->type = OP_REG; |
| 4147 | op->bytes = 2; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4148 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4149 | fetch_register_operand(op); |
| 4150 | break; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4151 | case OpCL: |
| 4152 | op->bytes = 1; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4153 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4154 | break; |
| 4155 | case OpImmByte: |
| 4156 | rc = decode_imm(ctxt, op, 1, true); |
| 4157 | break; |
| 4158 | case OpOne: |
| 4159 | op->bytes = 1; |
| 4160 | op->val = 1; |
| 4161 | break; |
| 4162 | case OpImm: |
| 4163 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); |
| 4164 | break; |
Nadav Amit | 5e2c688 | 2012-12-06 21:55:10 -0200 | [diff] [blame] | 4165 | case OpImm64: |
| 4166 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); |
| 4167 | break; |
Avi Kivity | 28867ce | 2012-01-16 15:08:44 +0200 | [diff] [blame] | 4168 | case OpMem8: |
| 4169 | ctxt->memop.bytes = 1; |
| 4170 | goto mem_common; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4171 | case OpMem16: |
| 4172 | ctxt->memop.bytes = 2; |
| 4173 | goto mem_common; |
| 4174 | case OpMem32: |
| 4175 | ctxt->memop.bytes = 4; |
| 4176 | goto mem_common; |
| 4177 | case OpImmU16: |
| 4178 | rc = decode_imm(ctxt, op, 2, false); |
| 4179 | break; |
| 4180 | case OpImmU: |
| 4181 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); |
| 4182 | break; |
| 4183 | case OpSI: |
| 4184 | op->type = OP_MEM; |
| 4185 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
| 4186 | op->addr.mem.ea = |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4187 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4188 | op->addr.mem.seg = seg_override(ctxt); |
| 4189 | op->val = 0; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4190 | op->count = 1; |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4191 | break; |
| 4192 | case OpImmFAddr: |
| 4193 | op->type = OP_IMM; |
| 4194 | op->addr.mem.ea = ctxt->_eip; |
| 4195 | op->bytes = ctxt->op_bytes + 2; |
| 4196 | insn_fetch_arr(op->valptr, op->bytes, ctxt); |
| 4197 | break; |
| 4198 | case OpMemFAddr: |
| 4199 | ctxt->memop.bytes = ctxt->op_bytes + 2; |
| 4200 | goto mem_common; |
Avi Kivity | c191a7a | 2011-09-13 10:45:49 +0300 | [diff] [blame] | 4201 | case OpES: |
| 4202 | op->val = VCPU_SREG_ES; |
| 4203 | break; |
| 4204 | case OpCS: |
| 4205 | op->val = VCPU_SREG_CS; |
| 4206 | break; |
| 4207 | case OpSS: |
| 4208 | op->val = VCPU_SREG_SS; |
| 4209 | break; |
| 4210 | case OpDS: |
| 4211 | op->val = VCPU_SREG_DS; |
| 4212 | break; |
| 4213 | case OpFS: |
| 4214 | op->val = VCPU_SREG_FS; |
| 4215 | break; |
| 4216 | case OpGS: |
| 4217 | op->val = VCPU_SREG_GS; |
| 4218 | break; |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4219 | case OpImplicit: |
| 4220 | /* Special instructions do their own operand decoding. */ |
| 4221 | default: |
| 4222 | op->type = OP_NONE; /* Disable writeback. */ |
| 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | done: |
| 4227 | return rc; |
| 4228 | } |
| 4229 | |
Takuya Yoshikawa | ef5d75c | 2011-05-15 00:57:43 +0900 | [diff] [blame] | 4230 | 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] | 4231 | { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4232 | int rc = X86EMUL_CONTINUE; |
| 4233 | int mode = ctxt->mode; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4234 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4235 | bool op_prefix = false; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4236 | struct opcode opcode; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4237 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4238 | ctxt->memop.type = OP_NONE; |
| 4239 | ctxt->memopp = NULL; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4240 | ctxt->_eip = ctxt->eip; |
| 4241 | ctxt->fetch.start = ctxt->_eip; |
| 4242 | ctxt->fetch.end = ctxt->fetch.start + insn_len; |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4243 | if (insn_len > 0) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4244 | memcpy(ctxt->fetch.data, insn, insn_len); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4245 | |
| 4246 | switch (mode) { |
| 4247 | case X86EMUL_MODE_REAL: |
| 4248 | case X86EMUL_MODE_VM86: |
| 4249 | case X86EMUL_MODE_PROT16: |
| 4250 | def_op_bytes = def_ad_bytes = 2; |
| 4251 | break; |
| 4252 | case X86EMUL_MODE_PROT32: |
| 4253 | def_op_bytes = def_ad_bytes = 4; |
| 4254 | break; |
| 4255 | #ifdef CONFIG_X86_64 |
| 4256 | case X86EMUL_MODE_PROT64: |
| 4257 | def_op_bytes = 4; |
| 4258 | def_ad_bytes = 8; |
| 4259 | break; |
| 4260 | #endif |
| 4261 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4262 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4263 | } |
| 4264 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4265 | ctxt->op_bytes = def_op_bytes; |
| 4266 | ctxt->ad_bytes = def_ad_bytes; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4267 | |
| 4268 | /* Legacy prefixes. */ |
| 4269 | for (;;) { |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4270 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4271 | case 0x66: /* operand-size override */ |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4272 | op_prefix = true; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4273 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4274 | ctxt->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4275 | break; |
| 4276 | case 0x67: /* address-size override */ |
| 4277 | if (mode == X86EMUL_MODE_PROT64) |
| 4278 | /* switch between 4/8 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4279 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4280 | else |
| 4281 | /* switch between 2/4 bytes */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4282 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4283 | break; |
| 4284 | case 0x26: /* ES override */ |
| 4285 | case 0x2e: /* CS override */ |
| 4286 | case 0x36: /* SS override */ |
| 4287 | case 0x3e: /* DS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4288 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4289 | break; |
| 4290 | case 0x64: /* FS override */ |
| 4291 | case 0x65: /* GS override */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4292 | set_seg_override(ctxt, ctxt->b & 7); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4293 | break; |
| 4294 | case 0x40 ... 0x4f: /* REX */ |
| 4295 | if (mode != X86EMUL_MODE_PROT64) |
| 4296 | goto done_prefixes; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4297 | ctxt->rex_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4298 | continue; |
| 4299 | case 0xf0: /* LOCK */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4300 | ctxt->lock_prefix = 1; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4301 | break; |
| 4302 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4303 | case 0xf3: /* REP/REPE/REPZ */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4304 | ctxt->rep_prefix = ctxt->b; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4305 | break; |
| 4306 | default: |
| 4307 | goto done_prefixes; |
| 4308 | } |
| 4309 | |
| 4310 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 4311 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4312 | ctxt->rex_prefix = 0; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | done_prefixes: |
| 4316 | |
| 4317 | /* REX prefix. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4318 | if (ctxt->rex_prefix & 8) |
| 4319 | ctxt->op_bytes = 8; /* REX.W */ |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4320 | |
| 4321 | /* Opcode byte(s). */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4322 | opcode = opcode_table[ctxt->b]; |
Wei Yongjun | d3ad624 | 2010-08-05 16:34:39 +0800 | [diff] [blame] | 4323 | /* Two-byte opcode? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4324 | if (ctxt->b == 0x0f) { |
| 4325 | ctxt->twobyte = 1; |
Takuya Yoshikawa | e85a108 | 2011-07-30 18:01:26 +0900 | [diff] [blame] | 4326 | ctxt->b = insn_fetch(u8, ctxt); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4327 | opcode = twobyte_table[ctxt->b]; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4328 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4329 | ctxt->d = opcode.flags; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4330 | |
Takuya Yoshikawa | 9f4260e | 2012-04-30 17:48:25 +0900 | [diff] [blame] | 4331 | if (ctxt->d & ModRM) |
| 4332 | ctxt->modrm = insn_fetch(u8, ctxt); |
| 4333 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4334 | while (ctxt->d & GroupMask) { |
| 4335 | switch (ctxt->d & GroupMask) { |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4336 | case Group: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4337 | goffset = (ctxt->modrm >> 3) & 7; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4338 | opcode = opcode.u.group[goffset]; |
| 4339 | break; |
| 4340 | case GroupDual: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4341 | goffset = (ctxt->modrm >> 3) & 7; |
| 4342 | if ((ctxt->modrm >> 6) == 3) |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4343 | opcode = opcode.u.gdual->mod3[goffset]; |
| 4344 | else |
| 4345 | opcode = opcode.u.gdual->mod012[goffset]; |
| 4346 | break; |
| 4347 | case RMExt: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4348 | goffset = ctxt->modrm & 7; |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4349 | opcode = opcode.u.group[goffset]; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4350 | break; |
| 4351 | case Prefix: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4352 | if (ctxt->rep_prefix && op_prefix) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4353 | return EMULATION_FAILED; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4354 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4355 | switch (simd_prefix) { |
| 4356 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; |
| 4357 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; |
| 4358 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; |
| 4359 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; |
| 4360 | } |
| 4361 | break; |
Gleb Natapov | 045a282 | 2012-12-20 16:57:43 +0200 | [diff] [blame] | 4362 | case Escape: |
| 4363 | if (ctxt->modrm > 0xbf) |
| 4364 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; |
| 4365 | else |
| 4366 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; |
| 4367 | break; |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4368 | default: |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4369 | return EMULATION_FAILED; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4370 | } |
Avi Kivity | 4656164 | 2011-04-24 14:09:59 +0300 | [diff] [blame] | 4371 | |
Avi Kivity | b1ea50b | 2011-09-13 10:45:42 +0300 | [diff] [blame] | 4372 | ctxt->d &= ~(u64)GroupMask; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4373 | ctxt->d |= opcode.flags; |
Avi Kivity | 0d7cdee | 2011-03-29 11:34:38 +0200 | [diff] [blame] | 4374 | } |
| 4375 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4376 | ctxt->execute = opcode.u.execute; |
| 4377 | ctxt->check_perm = opcode.check_perm; |
| 4378 | ctxt->intercept = opcode.intercept; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4379 | |
| 4380 | /* Unrecognised? */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4381 | if (ctxt->d == 0 || (ctxt->d & Undefined)) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4382 | return EMULATION_FAILED; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4383 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4384 | if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4385 | return EMULATION_FAILED; |
Avi Kivity | d867162 | 2011-02-01 16:32:03 +0200 | [diff] [blame] | 4386 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4387 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
| 4388 | ctxt->op_bytes = 8; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4389 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4390 | if (ctxt->d & Op3264) { |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4391 | if (mode == X86EMUL_MODE_PROT64) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4392 | ctxt->op_bytes = 8; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4393 | else |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4394 | ctxt->op_bytes = 4; |
Avi Kivity | 7f9b4b7 | 2010-08-01 14:46:54 +0300 | [diff] [blame] | 4395 | } |
| 4396 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4397 | if (ctxt->d & Sse) |
| 4398 | ctxt->op_bytes = 16; |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4399 | else if (ctxt->d & Mmx) |
| 4400 | ctxt->op_bytes = 8; |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4401 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4402 | /* ModRM and SIB bytes. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4403 | if (ctxt->d & ModRM) { |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4404 | rc = decode_modrm(ctxt, &ctxt->memop); |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4405 | if (!ctxt->has_seg_override) |
| 4406 | set_seg_override(ctxt, ctxt->modrm_seg); |
| 4407 | } else if (ctxt->d & MemAbs) |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4408 | rc = decode_abs(ctxt, &ctxt->memop); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4409 | if (rc != X86EMUL_CONTINUE) |
| 4410 | goto done; |
| 4411 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4412 | if (!ctxt->has_seg_override) |
| 4413 | set_seg_override(ctxt, VCPU_SREG_DS); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4414 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4415 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4416 | |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4417 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
| 4418 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4419 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4420 | /* |
| 4421 | * Decode and fetch the source operand: register, memory |
| 4422 | * or immediate. |
| 4423 | */ |
Avi Kivity | 0fe5912 | 2011-09-13 10:45:47 +0300 | [diff] [blame] | 4424 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4425 | if (rc != X86EMUL_CONTINUE) |
| 4426 | goto done; |
| 4427 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4428 | /* |
| 4429 | * Decode and fetch the second source operand: register, memory |
| 4430 | * or immediate. |
| 4431 | */ |
Avi Kivity | 4dd6a57 | 2011-09-13 10:45:43 +0300 | [diff] [blame] | 4432 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
Avi Kivity | 39f21ee | 2010-08-18 19:20:21 +0300 | [diff] [blame] | 4433 | if (rc != X86EMUL_CONTINUE) |
| 4434 | goto done; |
| 4435 | |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4436 | /* Decode and fetch the destination operand: register or memory. */ |
Avi Kivity | a9945549 | 2011-09-13 10:45:41 +0300 | [diff] [blame] | 4437 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4438 | |
| 4439 | done: |
Avi Kivity | f09ed83 | 2011-09-13 10:45:40 +0300 | [diff] [blame] | 4440 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
| 4441 | ctxt->memopp->addr.mem.ea += ctxt->_eip; |
Avi Kivity | cb16c34 | 2011-06-19 19:21:11 +0300 | [diff] [blame] | 4442 | |
Takuya Yoshikawa | 1d2887e | 2011-07-30 18:03:34 +0900 | [diff] [blame] | 4443 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | dde7e6d12 | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4444 | } |
| 4445 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 4446 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
| 4447 | { |
| 4448 | return ctxt->d & PageTable; |
| 4449 | } |
| 4450 | |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4451 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
| 4452 | { |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4453 | /* The second termination condition only applies for REPE |
| 4454 | * and REPNE. Test if the repeat string operation prefix is |
| 4455 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 4456 | * corresponding termination condition according to: |
| 4457 | * - if REPE/REPZ and ZF = 0 then done |
| 4458 | * - if REPNE/REPNZ and ZF = 1 then done |
| 4459 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4460 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
| 4461 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) |
| 4462 | && (((ctxt->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4463 | ((ctxt->eflags & EFLG_ZF) == 0)) |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4464 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4465 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
| 4466 | return true; |
| 4467 | |
| 4468 | return false; |
| 4469 | } |
| 4470 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4471 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
| 4472 | { |
| 4473 | bool fault = false; |
| 4474 | |
| 4475 | ctxt->ops->get_fpu(ctxt); |
| 4476 | asm volatile("1: fwait \n\t" |
| 4477 | "2: \n\t" |
| 4478 | ".pushsection .fixup,\"ax\" \n\t" |
| 4479 | "3: \n\t" |
| 4480 | "movb $1, %[fault] \n\t" |
| 4481 | "jmp 2b \n\t" |
| 4482 | ".popsection \n\t" |
| 4483 | _ASM_EXTABLE(1b, 3b) |
Avi Kivity | 38e8a2d | 2012-04-22 15:12:50 +0300 | [diff] [blame] | 4484 | : [fault]"+qm"(fault)); |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4485 | ctxt->ops->put_fpu(ctxt); |
| 4486 | |
| 4487 | if (unlikely(fault)) |
| 4488 | return emulate_exception(ctxt, MF_VECTOR, 0, false); |
| 4489 | |
| 4490 | return X86EMUL_CONTINUE; |
| 4491 | } |
| 4492 | |
| 4493 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, |
| 4494 | struct operand *op) |
| 4495 | { |
| 4496 | if (op->type == OP_MM) |
| 4497 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); |
| 4498 | } |
| 4499 | |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4500 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
| 4501 | { |
| 4502 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; |
| 4503 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; |
| 4504 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" |
| 4505 | : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags) |
| 4506 | : "c"(ctxt->src2.val), [fastop]"S"(fop)); |
| 4507 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); |
| 4508 | return X86EMUL_CONTINUE; |
| 4509 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4510 | |
Takuya Yoshikawa | 7b105ca | 2011-05-15 01:00:52 +0900 | [diff] [blame] | 4511 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4512 | { |
Mathias Krause | 0225fb5 | 2012-08-30 01:30:16 +0200 | [diff] [blame] | 4513 | const struct x86_emulate_ops *ops = ctxt->ops; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4514 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4515 | int saved_dst_type = ctxt->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4516 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4517 | ctxt->mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 4518 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4519 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4520 | rc = emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 4521 | goto done; |
| 4522 | } |
| 4523 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4524 | /* LOCK prefix is allowed only with some instructions */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4525 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4526 | rc = emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 4527 | goto done; |
| 4528 | } |
| 4529 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4530 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4531 | rc = emulate_ud(ctxt); |
Avi Kivity | 081bca0 | 2010-08-26 11:06:15 +0300 | [diff] [blame] | 4532 | goto done; |
| 4533 | } |
| 4534 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4535 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
| 4536 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4537 | rc = emulate_ud(ctxt); |
| 4538 | goto done; |
| 4539 | } |
| 4540 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4541 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
Avi Kivity | 1253791 | 2011-03-29 11:41:27 +0200 | [diff] [blame] | 4542 | rc = emulate_nm(ctxt); |
| 4543 | goto done; |
| 4544 | } |
| 4545 | |
Avi Kivity | cbe2c9d | 2012-04-09 18:40:02 +0300 | [diff] [blame] | 4546 | if (ctxt->d & Mmx) { |
| 4547 | rc = flush_pending_x87_faults(ctxt); |
| 4548 | if (rc != X86EMUL_CONTINUE) |
| 4549 | goto done; |
| 4550 | /* |
| 4551 | * Now that we know the fpu is exception safe, we can fetch |
| 4552 | * operands from it. |
| 4553 | */ |
| 4554 | fetch_possible_mmx_operand(ctxt, &ctxt->src); |
| 4555 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); |
| 4556 | if (!(ctxt->d & Mov)) |
| 4557 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); |
| 4558 | } |
| 4559 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4560 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4561 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4562 | X86_ICPT_PRE_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4563 | if (rc != X86EMUL_CONTINUE) |
| 4564 | goto done; |
| 4565 | } |
| 4566 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4567 | /* Privileged instruction can be executed only in CPL=0 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4568 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
Avi Kivity | 35d3d4a | 2010-11-22 17:53:25 +0200 | [diff] [blame] | 4569 | rc = emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 4570 | goto done; |
| 4571 | } |
| 4572 | |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4573 | /* Instruction can only be executed in protected mode */ |
Gleb Natapov | 9d1b39a | 2012-09-03 15:24:27 +0300 | [diff] [blame] | 4574 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
Joerg Roedel | 8ea7d6a | 2011-04-04 12:39:26 +0200 | [diff] [blame] | 4575 | rc = emulate_ud(ctxt); |
| 4576 | goto done; |
| 4577 | } |
| 4578 | |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4579 | /* Do instruction specific permission checks */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4580 | if (ctxt->check_perm) { |
| 4581 | rc = ctxt->check_perm(ctxt); |
Joerg Roedel | d09beab | 2011-04-04 12:39:25 +0200 | [diff] [blame] | 4582 | if (rc != X86EMUL_CONTINUE) |
| 4583 | goto done; |
| 4584 | } |
| 4585 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4586 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4587 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4588 | X86_ICPT_POST_EXCEPT); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4589 | if (rc != X86EMUL_CONTINUE) |
| 4590 | goto done; |
| 4591 | } |
| 4592 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4593 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4594 | /* All REP prefixes have the same first termination condition */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4595 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4596 | ctxt->eip = ctxt->_eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4597 | goto done; |
| 4598 | } |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 4599 | } |
| 4600 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4601 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
| 4602 | rc = segmented_read(ctxt, ctxt->src.addr.mem, |
| 4603 | ctxt->src.valptr, ctxt->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 4604 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4605 | goto done; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4606 | ctxt->src.orig_val64 = ctxt->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4607 | } |
| 4608 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4609 | if (ctxt->src2.type == OP_MEM) { |
| 4610 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, |
| 4611 | &ctxt->src2.val, ctxt->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 4612 | if (rc != X86EMUL_CONTINUE) |
| 4613 | goto done; |
| 4614 | } |
| 4615 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4616 | if ((ctxt->d & DstMask) == ImplicitOps) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4617 | goto special_insn; |
| 4618 | |
| 4619 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4620 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4621 | /* optimisation - avoid slow emulated read if Mov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4622 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
| 4623 | &ctxt->dst.val, ctxt->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 4624 | if (rc != X86EMUL_CONTINUE) |
| 4625 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4626 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4627 | ctxt->dst.orig_val = ctxt->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 4628 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4629 | special_insn: |
| 4630 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4631 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
| 4632 | rc = emulator_check_intercept(ctxt, ctxt->intercept, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4633 | X86_ICPT_POST_MEMACCESS); |
Avi Kivity | c4f035c | 2011-04-04 12:39:22 +0200 | [diff] [blame] | 4634 | if (rc != X86EMUL_CONTINUE) |
| 4635 | goto done; |
| 4636 | } |
| 4637 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4638 | if (ctxt->execute) { |
Avi Kivity | e28bbd4 | 2013-01-04 16:18:48 +0200 | [diff] [blame] | 4639 | if (ctxt->d & Fastop) { |
| 4640 | void (*fop)(struct fastop *) = (void *)ctxt->execute; |
| 4641 | rc = fastop(ctxt, fop); |
| 4642 | if (rc != X86EMUL_CONTINUE) |
| 4643 | goto done; |
| 4644 | goto writeback; |
| 4645 | } |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4646 | rc = ctxt->execute(ctxt); |
Avi Kivity | ef65c88 | 2010-07-29 15:11:51 +0300 | [diff] [blame] | 4647 | if (rc != X86EMUL_CONTINUE) |
| 4648 | goto done; |
| 4649 | goto writeback; |
| 4650 | } |
| 4651 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4652 | if (ctxt->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4653 | goto twobyte_insn; |
| 4654 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4655 | switch (ctxt->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4656 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 4657 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4658 | goto cannot_emulate; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4659 | ctxt->dst.val = (s32) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4660 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4661 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4662 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4663 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4664 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4665 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4666 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 4667 | break; |
Avi Kivity | 3d9e77d | 2010-08-01 12:41:59 +0300 | [diff] [blame] | 4668 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4669 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
Mohammed Gamal | 34698d8 | 2010-08-04 14:41:04 +0300 | [diff] [blame] | 4670 | break; |
Takuya Yoshikawa | e4f973a | 2011-05-29 21:59:09 +0900 | [diff] [blame] | 4671 | rc = em_xchg(ctxt); |
| 4672 | break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4673 | case 0x98: /* cbw/cwde/cdqe */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4674 | switch (ctxt->op_bytes) { |
| 4675 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; |
| 4676 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; |
| 4677 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; |
Wei Yongjun | e8b6fa7 | 2010-08-18 16:43:13 +0800 | [diff] [blame] | 4678 | } |
| 4679 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4680 | case 0xcc: /* int3 */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4681 | rc = emulate_int(ctxt, 3); |
| 4682 | break; |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4683 | case 0xcd: /* int n */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4684 | rc = emulate_int(ctxt, ctxt->src.val); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4685 | break; |
| 4686 | case 0xce: /* into */ |
Takuya Yoshikawa | 5c5df76 | 2011-05-29 22:02:55 +0900 | [diff] [blame] | 4687 | if (ctxt->eflags & EFLG_OF) |
| 4688 | rc = emulate_int(ctxt, 4); |
Mohammed Gamal | 6e154e5 | 2010-08-04 14:38:06 +0300 | [diff] [blame] | 4689 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4690 | case 0xe9: /* jmp rel */ |
Takuya Yoshikawa | db5b076 | 2011-05-29 21:56:26 +0900 | [diff] [blame] | 4691 | case 0xeb: /* jmp rel short */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4692 | jmp_rel(ctxt, ctxt->src.val); |
| 4693 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 4694 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4695 | case 0xf4: /* hlt */ |
Avi Kivity | 6c3287f | 2011-04-20 15:43:05 +0300 | [diff] [blame] | 4696 | ctxt->ops->halt(ctxt); |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 4697 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4698 | case 0xf5: /* cmc */ |
| 4699 | /* complement carry flag from eflags reg */ |
| 4700 | ctxt->eflags ^= EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4701 | break; |
| 4702 | case 0xf8: /* clc */ |
| 4703 | ctxt->eflags &= ~EFLG_CF; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 4704 | break; |
Mohammed Gamal | 8744aa9 | 2010-08-05 15:42:49 +0300 | [diff] [blame] | 4705 | case 0xf9: /* stc */ |
| 4706 | ctxt->eflags |= EFLG_CF; |
| 4707 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4708 | case 0xfc: /* cld */ |
| 4709 | ctxt->eflags &= ~EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4710 | break; |
| 4711 | case 0xfd: /* std */ |
| 4712 | ctxt->eflags |= EFLG_DF; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 4713 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4714 | default: |
| 4715 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4716 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4717 | |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4718 | if (rc != X86EMUL_CONTINUE) |
| 4719 | goto done; |
| 4720 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4721 | writeback: |
Takuya Yoshikawa | adddcec | 2011-05-02 02:26:23 +0900 | [diff] [blame] | 4722 | rc = writeback(ctxt); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 4723 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4724 | goto done; |
| 4725 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4726 | /* |
| 4727 | * restore dst type in case the decoding will be reused |
| 4728 | * (happens for string instruction ) |
| 4729 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4730 | ctxt->dst.type = saved_dst_type; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4731 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4732 | if ((ctxt->d & SrcMask) == SrcSI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4733 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 4734 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4735 | if ((ctxt->d & DstMask) == DstDI) |
Gleb Natapov | f3bd64c | 2012-09-03 15:24:28 +0300 | [diff] [blame] | 4736 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 4737 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4738 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4739 | unsigned int count; |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4740 | struct read_cache *r = &ctxt->io_read; |
Gleb Natapov | b3356bf | 2012-09-03 15:24:29 +0300 | [diff] [blame] | 4741 | if ((ctxt->d & SrcMask) == SrcSI) |
| 4742 | count = ctxt->src.count; |
| 4743 | else |
| 4744 | count = ctxt->dst.count; |
| 4745 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), |
| 4746 | -count); |
Gleb Natapov | 3e2f65d | 2010-08-25 12:47:42 +0300 | [diff] [blame] | 4747 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4748 | if (!string_insn_completed(ctxt)) { |
| 4749 | /* |
| 4750 | * Re-enter guest when pio read ahead buffer is empty |
| 4751 | * or, if it is not used, after each 1024 iteration. |
| 4752 | */ |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4753 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4754 | (r->end == 0 || r->end != r->pos)) { |
| 4755 | /* |
| 4756 | * Reset read cache. Usually happens before |
| 4757 | * decode, but since instruction is restarted |
| 4758 | * we have to do it here. |
| 4759 | */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4760 | ctxt->mem_read.end = 0; |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4761 | writeback_registers(ctxt); |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4762 | return EMULATION_RESTART; |
| 4763 | } |
| 4764 | goto done; /* skip rip writeback */ |
Avi Kivity | 0fa6ccb | 2010-08-17 11:22:17 +0300 | [diff] [blame] | 4765 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4766 | } |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4767 | |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4768 | ctxt->eip = ctxt->_eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4769 | |
| 4770 | done: |
Avi Kivity | da9cb57 | 2010-11-22 17:53:21 +0200 | [diff] [blame] | 4771 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 4772 | ctxt->have_exception = true; |
Joerg Roedel | 775fde8 | 2011-04-04 12:39:24 +0200 | [diff] [blame] | 4773 | if (rc == X86EMUL_INTERCEPTED) |
| 4774 | return EMULATION_INTERCEPTED; |
| 4775 | |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4776 | if (rc == X86EMUL_CONTINUE) |
| 4777 | writeback_registers(ctxt); |
| 4778 | |
Gleb Natapov | d2ddd1c | 2010-08-25 12:47:43 +0300 | [diff] [blame] | 4779 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4780 | |
| 4781 | twobyte_insn: |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4782 | switch (ctxt->b) { |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4783 | case 0x09: /* wbinvd */ |
Clemens Noss | cfb2237 | 2011-04-21 21:16:05 +0200 | [diff] [blame] | 4784 | (ctxt->ops->wbinvd)(ctxt); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4785 | break; |
| 4786 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4787 | case 0x0d: /* GrpP (prefetch) */ |
| 4788 | case 0x18: /* Grp16 (prefetch/nop) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4789 | break; |
| 4790 | case 0x20: /* mov cr, reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4791 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4792 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4793 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4794 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4795 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4796 | case 0x40 ... 0x4f: /* cmov */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4797 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
| 4798 | if (!test_cc(ctxt->b, ctxt->eflags)) |
| 4799 | ctxt->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4800 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 4801 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4802 | if (test_cc(ctxt->b, ctxt->eflags)) |
| 4803 | jmp_rel(ctxt, ctxt->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 4804 | break; |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4805 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4806 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
Wei Yongjun | ee45b58 | 2010-08-06 17:10:07 +0800 | [diff] [blame] | 4807 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 4808 | case 0xae: /* clflush */ |
| 4809 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4810 | case 0xb6 ... 0xb7: /* movzx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4811 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4812 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4813 | : (u16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4814 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4815 | case 0xbe ... 0xbf: /* movsx */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4816 | ctxt->dst.bytes = ctxt->op_bytes; |
Avi Kivity | 361cad2 | 2012-06-11 19:40:15 +0300 | [diff] [blame] | 4817 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4818 | (s16) ctxt->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4819 | break; |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4820 | case 0xc0 ... 0xc1: /* xadd */ |
Avi Kivity | a31b9ce | 2011-09-07 16:41:35 +0300 | [diff] [blame] | 4821 | emulate_2op_SrcV(ctxt, "add"); |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4822 | /* Write back the register source. */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4823 | ctxt->src.val = ctxt->dst.orig_val; |
| 4824 | write_register_operand(&ctxt->src); |
Wei Yongjun | 92f738a | 2010-08-17 09:19:34 +0800 | [diff] [blame] | 4825 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4826 | case 0xc3: /* movnti */ |
Avi Kivity | 9dac77f | 2011-06-01 15:34:25 +0300 | [diff] [blame] | 4827 | ctxt->dst.bytes = ctxt->op_bytes; |
| 4828 | ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val : |
| 4829 | (u64) ctxt->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 4830 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 4831 | default: |
| 4832 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4833 | } |
Avi Kivity | 7d9ddae | 2010-08-30 17:12:28 +0300 | [diff] [blame] | 4834 | |
| 4835 | if (rc != X86EMUL_CONTINUE) |
| 4836 | goto done; |
| 4837 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4838 | goto writeback; |
| 4839 | |
| 4840 | cannot_emulate: |
Gleb Natapov | a0c0ab2 | 2011-03-28 16:57:49 +0200 | [diff] [blame] | 4841 | return EMULATION_FAILED; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4842 | } |
Avi Kivity | dd856ef | 2012-08-27 23:46:17 +0300 | [diff] [blame] | 4843 | |
| 4844 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4845 | { |
| 4846 | invalidate_registers(ctxt); |
| 4847 | } |
| 4848 | |
| 4849 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) |
| 4850 | { |
| 4851 | writeback_registers(ctxt); |
| 4852 | } |